[OT]2011年度最佳代码

23 views
Skip to first unread message

jimmy xu

unread,
Aug 4, 2011, 11:38:50 AM8/4/11
to shlug
try
{
    if(you.believe(it)==true || you.believe(it)==false)
{
        i.believe(it)
    }
}
catch(Exception e)
{
    throw new Exception("It's a miracle!")
}
finally
{
    it.justHappened();
}


--
Jimmy Xu 
Just Follow My Heart!!

小马xiaoma

unread,
Aug 4, 2011, 11:49:34 AM8/4/11
to sh...@googlegroups.com
 if(you.believe(it)==true || you.believe(it)==false)
这句会被编译器优化掉的。

Zhang Cheng

unread,
Aug 4, 2011, 1:06:30 PM8/4/11
to sh...@googlegroups.com
不一定,如果声明you为volatile,或者you的believe()方法返回值编译时不可预测,那编译器就不能优化了。

2011/8/4 小马xiaoma <cnxi...@gmail.com>



--
Best Regards,
Cheng

机械唯物主义 : linjunhalida

unread,
Aug 4, 2011, 7:46:58 PM8/4/11
to sh...@googlegroups.com
以及believe可能会有副作用.
这里面调用了2次believe, 也就是2次拷问. 如果第一次为false, 第二次是true, 那么就会只执行: it.justHappened..

2011/8/5 Zhang Cheng <steph...@googlemail.com>:
> 不一定,如果声明you为volatile,或者you的believe()方法返回值编译时不可预测,那编译器就不能优化了。
>

Xiangfu Liu

unread,
Aug 4, 2011, 9:27:29 PM8/4/11
to sh...@googlegroups.com, 小马xiaoma
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 08/04/2011 11:49 PM, 小马xiaoma wrote:
> if(you.believe(it)==true || you.believe(it)==false)
> 这句会被编译器优化掉的。

then send one patch :)

- --
Best Regards
Xiangfu Liu
http://www.openmobilefree.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk47RwEACgkQRRAEFRxkgLSDJwCfc1phqmhNtLcNzPdYn/xre6pI
eMMAn1Mz4i5ZHL67anxwcLvRwt6cUKYQ
=F+6z
-----END PGP SIGNATURE-----

Xiangfu Liu

unread,
Aug 4, 2011, 9:28:09 PM8/4/11
to sh...@googlegroups.com, jimmy xu
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

转载了一下: http://www.openmobilefree.net/?p=972

如有不妥,请告知 :)

great code.

- --
Best Regards
Xiangfu Liu
http://www.openmobilefree.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk47RykACgkQRRAEFRxkgLTdxACgmA/TP2+WyMHnfjoHofAaSPPd
d34An0ULaDmCpImok7auZ9Gue4l7m1Bf
=AOOs
-----END PGP SIGNATURE-----

Yico Gaga

unread,
Aug 4, 2011, 9:42:25 PM8/4/11
to sh...@googlegroups.com
至于你信不信,反正我是信了.
--
Make Things Right !
Thx 4 Help !

Xiangfu Liu

unread,
Aug 4, 2011, 9:50:34 PM8/4/11
to sh...@googlegroups.com, Yico Gaga
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 08/05/2011 09:42 AM, Yico Gaga wrote:
> 至于你信不信,反正我是信了.

来个C的:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/fcntl.h>

int believe(int it, char * you)
{
int random;
int c;

random = open("/dev/random", O_RDONLY);
read(random, &c, sizeof(c));

if(!strcmp(you, "people")) {
printf("It doesn’t matter if you guys believe or not\n");
return c;
}

close(random);

printf("I believe!\n");
return 1;
}

int main()
{
int ret;
int it; /* you know */

char * you = "people";
char * I;

switch(believe(it, you)) {
default:
ret = believe(it, I);
}

if(ret)
printf("It's a miracle!\n");

printf("It just happened!\n");
return 0;
}

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk47TGoACgkQRRAEFRxkgLRc3QCgwAArZakU15fy7Gzt6CoaDs5d
fgIAoLmp1KzXHtg+oFklBGuFWV0sR04c
=LjoC
-----END PGP SIGNATURE-----

ghosTM55

unread,
Aug 4, 2011, 9:54:07 PM8/4/11
to sh...@googlegroups.com
2011/8/5 Xiangfu Liu <xia...@openmobilefree.net>:

你们真是闲得蛋疼啊...

--
Thomas
Shanghai Linux User Group

http://ghosTunix.org
Twitter: @ghosTM55

Moore

unread,
Aug 5, 2011, 12:32:43 AM8/5/11
to Shanghai Linux User Group
it 没有初始值,而且在 believe 函数里it 没有用到。

I 也没有初始值,而且全局用不到 char *I.

Xiangfu Liu

unread,
Aug 5, 2011, 1:06:56 AM8/5/11
to sh...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 08/05/2011 12:32 PM, Moore wrote:
> it 没有初始值,而且在 believe 函数里it 没有用到。
>

有注释

> I 也没有初始值,而且全局用不到 char *I.

写的太清楚不太好吧~ :)

- --
Best Regards
Xiangfu Liu
http://www.openmobilefree.net

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk47enAACgkQRRAEFRxkgLRkHQCgkgRgOX7Ms74b5GyUS+ptjgqs
2SQAoIf/VQM8R8qu7MOiO3g/LG7SEnPK
=94iv
-----END PGP SIGNATURE-----

yang xiaoyong

unread,
Aug 5, 2011, 7:11:18 AM8/5/11
to sh...@googlegroups.com
In [23]: try:
   ....:     belive_or_not
   ....:     print "I belive"
   ....: except Exception:
   ....:     raise MiracleException("It's a miracle")
   ....: finally:
   ....:     print "It's just happend."
   ....:     
我来个python的 :)

2011/8/5 Xiangfu Liu <xia...@openmobilefree.net>



--

Twitter & weibo: @yangxiaoyong

Zhang Cheng

unread,
Aug 5, 2011, 9:11:45 AM8/5/11
to sh...@googlegroups.com



2011/8/5 Xiangfu Liu <xia...@openmobilefree.net>

       random = open("/dev/random", O_RDONLY);
       read(random, &c, sizeof(c));

       if(!strcmp(you, "people")) {
               printf("It doesn’t matter if you guys believe or not\n");

在函数返回前,random没有被close()。这个函数被执行多次会导致fd太多,把程序挂掉。
 
               return c;
       }

       close(random);



--
Best Regards,
Cheng

Xiangfu Liu

unread,
Aug 5, 2011, 9:26:26 AM8/5/11
to sh...@googlegroups.com, Zhang Cheng
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 08/05/2011 09:11 PM, Zhang Cheng wrote:
>
> if(!strcmp(you, "people")) {
> printf("It doesn’t matter if you guys believe or not\n");
>
>
> 在函数返回前,random没有被close()。这个函数被执行多次会导致fd太多,把程序挂掉。

嗯。这是个问题,

- --
Best Regards
Xiangfu Liu
http://www.openmobilefree.net

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk4774IACgkQRRAEFRxkgLSEVACdG+DW+Zz+iAlvbGixJIzGu+Gp
K48AoISu3el4YvPiJOLmg0vnaXBV0Dqv
=XyVG
-----END PGP SIGNATURE-----

Xiangfu Liu

unread,
Aug 5, 2011, 9:28:43 AM8/5/11
to sh...@googlegroups.com, Zhang Cheng
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 08/05/2011 09:11 PM, Zhang Cheng wrote:
>
> 在函数返回前,random没有被close()。这个函数被执行多次会导致fd太多,把程序挂掉。

new version: http://www.openmobilefree.net/?p=972

- --
Best Regards
Xiangfu Liu
http://www.openmobilefree.net

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk478AsACgkQRRAEFRxkgLSucQCfRkcKOtZgusdUGbQXbd1Nhq7g
ccgAn3+E5RhNLgiQlcwXIw05WeAs780M
=L4Pt
-----END PGP SIGNATURE-----

Message has been deleted

Xiangfu Liu

unread,
Aug 7, 2011, 1:14:54 AM8/7/11
to sh...@googlegroups.com, Hugh
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 08/07/2011 12:51 PM, Hugh wrote:
> #include <铁道部.h>

then we don't needs those code :)
int it;
char * I;

- --
Best Regards
Xiangfu Liu
http://www.openmobilefree.net

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk4+H04ACgkQRRAEFRxkgLRqsACfUu+dKk42aGAaQ2d0ha3dg2dO
SCgAn1MbV6g+SNpyf7F9ZeXxTpYQk63J
=GcQl
-----END PGP SIGNATURE-----

Bryton Lee

unread,
Aug 7, 2011, 9:04:49 PM8/7/11
to sh...@googlegroups.com, Hugh
估计你们都是"无证程序员"吧, 写出这么优异的代码!

2011/8/7 Xiangfu Liu <xia...@openmobilefree.net>



--
Best Regards

Bryton.Lee


Serenade

unread,
Aug 7, 2011, 9:25:40 PM8/7/11
to sh...@googlegroups.com
没有unicode版本

2011/8/8 Bryton Lee <bryto...@gmail.com>:

--
Thanx & Regards,
小溪同学

Reply all
Reply to author
Forward
0 new messages