(本文数据较多,在rss reader里面可能可读性不是很好,可考虑直接在线阅读) 昨天看到pipitu的这篇blog,觉得蛮有意思,文章比较了python和java的list performance,只可惜没有include ruby 1.8和ruby 1.9,不然结论会更有悬念一点,呵呵. 由于无法营造一致的软硬件环境,也不愿意重复pipitu关于python和java部分的工作,这里就只好先简单benchmark(如果可以算是的话)一下ruby 1.8和1.9里的list (array) performance了,也许pipitu以后有空更新include ruby也说不定:) 先看一下我们用的ruby的版本: [cc lang=”bash”] $ ruby -v ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin9] [/cc] [cc lang=”bash”] $ ruby1.9 -v ruby 1.9.1p376 (2009-12-07 revision 26041) [i386-darwin9] [/cc] 还有os: [cc lang=”bash” nowrap=”false”] $ uname -a Darwin macbook.local 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:55:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_I386 i386 [/cc] benchmark的方法和pipitu的python sample类似,就是往空的list里面循环n次append,由于循环也被引入,其performance也值得考虑,因为众所周知ruby里面做一件事犹如孔乙己的茴字写法一样有n种办法,其performance也各不一样,说不定pipitu以后会和python, java进行横向,咱还是挑相对快一点的好~ 不过话说回来,再怎么的精挑细选,java那边还是基本没有悬念,真正有悬念的,我们都心知肚明,呵呵。 所以真正运动之前先来点热身运动,看看哪个循环在我这相对要快: [cc lang=”ruby”] require ‘benchmark’ n = ARGV.empty? ? 10 6 : 10 ARGV[0].to_i puts “n=#{n}” Benchmark.bmbm do |b| b.report(“for:”) { for i in 1..n ; a = “0”; end } b.report(“times:”) { n.times do ; a = “0”; end } b.report(“upto:”) { 1.upto(n) do ; a = “0”; end } b.report(“downto:”) { n.downto(1) do ; a = “0”; end } end [/cc] 以上是我所想到的比较常见的4种茴字写法,程序默认取n=10^6进行循环,也可以接受一个作为幂指数的命令行参数,Benchmark的bmbm方法会先dry run一遍所有test作为rehearsal,等memory和garbage collection的状态相对stable了以后才进行真正的benchmark,这样先运行和后运行的report会相对公平一点。 默认运行( n=10^6 )的结果如下: [cc lang=”bash”] n=1000000 Rehearsal ——————————————- for: 0.310000 0.000000 0.310000 ( 0.313392) times: 0.290000 0.000000 0.290000 ( 0.297268) upto: 0.300000 0.000000 0.300000 ( 0.300311) downto: 0.310000 0.000000 0.310000 ( 0.304146) ———————————- total: 1.210000sec user system total real for: 0.310000 0.000000 0.310000 ( 0.313129) times: 0.290000 0.000000 0.290000 ( 0.297470) upto: 0.300000 0.000000 0.300000 ( 0.302154) downto: 0.310000 0.000000 0.310000 ( 0.305562) [/cc] 一次完整运行( n=10~10^8 )的数据结果如下: [cc lang=”bash”] n=10 Rehearsal ——————————————- for: 0.000000 0.000000 0.000000 ( 0.000018) times: 0.000000 0.000000 0.000000 ( 0.000011) upto: 0.000000 0.000000 0.000000 ( 0.000047) downto: 0.000000 0.000000 0.000000 ( 0.000053) ———————————- total: 0.000000sec user system total real for: 0.000000 0.000000 0.000000 ( 0.000014) times: 0.000000 0.000000 0.000000 ( 0.000011) upto: 0.000000 0.000000 0.000000 ( 0.000011) downto: 0.000000 0.000000 0.000000 ( 0.000011) ===================================================== n=100 Rehearsal ——————————————- for: 0.000000 0.000000 0.000000 ( 0.000041) times: 0.000000 0.000000 0.000000 ( 0.000031) upto: 0.000000 0.000000 0.000000 ( 0.000033) downto: 0.000000 0.000000 0.000000 ( 0.000040) ———————————- total: 0.000000sec user system total real for: 0.000000 0.000000 0.000000 ( 0.000035) times: 0.000000 0.000000 0.000000 ( 0.000030) upto: 0.000000 0.000000 0.000000 ( 0.000031) downto: 0.000000 0.000000 0.000000 ( 0.000031) ===================================================== n=1000 Rehearsal ——————————————- for: 0.000000 0.000000 0.000000 ( 0.000367) times: 0.000000 0.000000 0.000000 ( 0.000315) upto: 0.000000 0.000000 0.000000 ( 0.000319) downto: 0.000000 0.000000 0.000000 ( 0.000854) ———————————- total: 0.000000sec user system total real for: 0.000000 0.000000 0.000000 ( 0.000273) times: 0.000000 0.000000 0.000000 ( 0.000250) upto: 0.000000 0.000000 0.000000 ( 0.000254) downto: 0.000000 0.000000 0.000000 ( 0.000258) ===================================================== n=10000 Rehearsal ——————————————- for: 0.000000 0.000000 0.000000 ( 0.003388) times: 0.010000 0.000000 0.010000 ( 0.003056) upto: 0.000000 0.000000 0.000000 ( 0.003006) downto: 0.000000 0.000000 0.000000 ( 0.003084) ———————————- total: 0.010000sec user system total real for: 0.010000 0.000000 0.010000 ( 0.003114) times: 0.000000 0.000000 0.000000 ( 0.002955) upto: 0.000000 0.000000 0.000000 ( 0.003047) downto: 0.010000 0.000000 0.010000 ( 0.004650) ===================================================== n=100000 Rehearsal ——————————————- for: 0.030000 0.000000 0.030000 ( 0.032892) times: 0.030000 0.000000 0.030000 ( 0.029808) upto: 0.030000 0.000000 0.030000 ( 0.031190) downto: 0.030000 0.000000 0.030000 ( 0.030379) ———————————- total: 0.120000sec user system total real for: 0.030000 0.000000 0.030000 ( 0.033382) times: 0.030000 0.000000 0.030000 ( 0.029139) upto: 0.030000 0.000000 0.030000 ( 0.031608) downto: 0.030000 0.000000 0.030000 ( 0.030498) ===================================================== n=1000000 Rehearsal ——————————————- for: 0.310000 0.000000 0.310000 ( 0.313392) times: 0.290000 0.000000 0.290000 ( 0.297268) upto: 0.300000 0.000000 0.300000 ( 0.300311) downto: 0.310000 0.000000 0.310000 ( 0.304146) ———————————- total: 1.210000sec user system total real for: 0.310000 0.000000 0.310000 ( 0.313129) times: 0.290000 0.000000 0.290000 ( 0.297470) upto: 0.300000 0.000000 0.300000 ( 0.302154) downto: 0.310000 0.000000 0.310000 ( 0.305562) ===================================================== n=10000000 Rehearsal ——————————————- for: 3.090000 0.000000 3.090000 ( 3.109693) times: 2.940000 0.010000 2.950000 ( 2.946662) upto: 2.990000 0.010000 3.000000 ( 3.003812) downto: 3.010000 0.000000 3.010000 ( 3.021900) ——————————— total: 12.050000sec user system total real for: 3.110000 0.010000 3.120000 ( 3.119928) times: 2.950000 0.010000 2.960000 ( 2.992141) upto: 3.000000 0.010000 3.010000 ( 3.044853) downto: 3.020000 0.000000 3.020000 ( 3.036987) ===================================================== n=100000000 Rehearsal ——————————————- for: 30.950000 0.070000 31.020000 ( 31.139756) times: 29.360000 0.060000 29.420000 ( 29.543839) upto: 29.890000 0.070000 29.960000 ( 30.102346) downto: 30.080000 0.070000 30.150000 ( 30.271642) ——————————– total: 120.550000sec user system total real for: 31.130000 0.080000 31.210000 ( 31.359814) times: 29.420000 0.050000 29.470000 ( 29.630556) upto: 30.050000 0.060000 30.110000 ( 30.197589) downto: 30.180000 0.050000 30.230000 ( 30.361343) ===================================================== [/cc] 由于精力有限,就不做多次运行取平均值这种相对无聊的事情了,结果虽然不严谨也无法真正判断出究竟谁最快(times和upto,downto是在伯仲之间),但至少可以看出for in range是相对最慢的。 换ruby1.9后的运行结果与上基本一致,详细数据如下: [cc lang=”bash”] n=10 Rehearsal ——————————————- for: 0.000000 0.000000 0.000000 ( 0.000029) times: 0.000000 0.000000 0.000000 ( 0.000050) upto: 0.000000 0.000000 0.000000 ( 0.000014) downto: 0.000000 0.000000 0.000000 ( 0.000013) ———————————- total: 0.000000sec user system total real for: 0.000000 0.000000 0.000000 ( 0.000014) times: 0.000000 0.000000 0.000000 ( 0.000011) upto: 0.000000 0.000000 0.000000 ( 0.000012) downto: 0.000000 0.000000 0.000000 ( 0.000012) ===================================================== n=100 Rehearsal ——————————————- for: 0.000000 0.000000 0.000000 ( 0.000061) times: 0.000000 0.000000 0.000000 ( 0.000103) upto: 0.000000 0.000000 0.000000 ( 0.000078) downto: 0.000000 0.000000 0.000000 ( 0.000060) ———————————- total: 0.000000sec user system total real for: 0.000000 0.000000 0.000000 ( 0.000066) times: 0.000000 0.000000 0.000000 ( 0.000061) upto: 0.000000 0.000000 0.000000 ( 0.000061) downto: 0.000000 0.000000 0.000000 ( 0.000061) ===================================================== n=1000 Rehearsal ——————————————- for: 0.000000 0.000000 0.000000 ( 0.000465) times: 0.000000 0.000000 0.000000 ( 0.000461) upto: 0.000000 0.000000 0.000000 ( 0.000477) downto: 0.000000 0.000000 0.000000 ( 0.000428) ———————————- total: 0.000000sec user system total real for: 0.000000 0.000000 0.000000 ( 0.000388) times: 0.000000 0.000000 0.000000 ( 0.000339) upto: 0.000000 0.000000 0.000000 ( 0.000341) downto: 0.000000 0.000000 0.000000 ( 0.000339) ===================================================== n=10000 Rehearsal ——————————————- for: 0.000000 0.000000 0.000000 ( 0.004486) times: 0.010000 0.000000 0.010000 ( 0.003675) upto: 0.000000 0.000000 0.000000 ( 0.004407) downto: 0.000000 0.000000 0.000000 ( 0.004121) ———————————- total: 0.010000sec user system total real for: 0.010000 0.000000 0.010000 ( 0.003961) times: 0.000000 0.000000 0.000000 ( 0.005113) upto: 0.010000 0.000000 0.010000 ( 0.003592) downto: 0.000000 0.000000 0.000000 ( 0.003592) ===================================================== n=100000 Rehearsal ——————————————- for: 0.040000 0.000000 0.040000 ( 0.042573) times: 0.030000 0.000000 0.030000 ( 0.039094) upto: 0.030000 0.000000 0.030000 ( 0.041495) downto: 0.040000 0.000000 0.040000 ( 0.049316) ———————————- total: 0.140000sec user system total real for: 0.040000 0.000000 0.040000 ( 0.040422) times: 0.040000 0.000000 0.040000 ( 0.037030) upto: 0.040000 0.000000 0.040000 ( 0.041921) downto: 0.040000 0.000000 0.040000 ( 0.039868) ===================================================== n=1000000 Rehearsal ——————————————- for: 0.390000 0.000000 0.390000 ( 0.401887) times: 0.370000 0.000000 0.370000 ( 0.376656) upto: 0.370000 0.010000 0.380000 ( 0.384750) downto: 0.370000 0.000000 0.370000 ( 0.380184) ———————————- total: 1.510000sec user system total real for: 0.400000 0.000000 0.400000 ( 0.411929) times: 0.370000 0.000000 0.370000 ( 0.386443) upto: 0.370000 0.010000 0.380000 ( 0.379820) downto: 0.370000 0.000000 0.370000 ( 0.379068) ===================================================== n=10000000 Rehearsal ——————————————- for: 3.920000 0.020000 3.940000 ( 4.021434) times: 3.690000 0.030000 3.720000 ( 3.844118) upto: 3.710000 0.030000 3.740000 ( 3.844035) downto: 3.700000 0.020000 3.720000 ( 3.795330) ——————————— total: 15.120000sec user system total real for: 3.910000 0.020000 3.930000 ( 4.003016) times: 3.670000 0.020000 3.690000 ( 3.749495) upto: 3.700000 0.020000 3.720000 ( 3.919785) downto: 3.690000 0.030000 3.720000 ( 3.789975) ===================================================== n=100000000 Rehearsal ——————————————- for: 39.260000 0.240000 39.500000 ( 40.293460) times: 36.700000 0.210000 36.910000 ( 37.450910) upto: 37.020000 0.260000 37.280000 ( 38.963391) downto: 36.940000 0.230000 37.170000 ( 37.806189) ——————————– total: 150.860000sec user system total real for: 39.230000 0.240000 39.470000 ( 40.471694) times: 36.850000 0.240000 37.090000 ( 38.302817) upto: 36.870000 0.230000 37.100000 ( 37.802815) downto: 36.970000 0.220000 37.190000 ( 38.052300) ===================================================== [/cc] 有趣的是ruby1.9的循环操作性能竟然普遍都是负增长,这倒为我们之后的list performance比较埋下了伏笔。 热身运动到此结束,似乎有喧宾夺主之嫌,所以赶紧搞个part 2…,下篇文章我们会用上面循环性能测试的疑似最佳表现者times来进行list append性能测试,敬请期待。
Update-29
1. Again, Back to Normal -> abnormal 2. 不管normal还是abnormal,牙痛还是牙肿,life还是要goes on,update还要不断,虽然精彩常断…… 3. 春节回家亚克西,除夕春晚亚克西,holiday week亚克西,全靠党的政策亚克西;什么亚克西,什么亚克西啊,党中央的政策亚克西 4. 手机号码换了,这里也不怕再啰嗦一遍的…… 至于获得详情的方法这里不再啰嗦,不是什么都可以啰嗦的,看官需要培养好眼力,鄙人则需要锻炼好听力…… 经验和实践同时表明,换号码的成本不小,以后3G慢慢普及,移动TD众望所归杯具了之后,应该已经可以携号转网了吧,我可不想再折腾了…… 5. crasher, which means uninvited guest, you don’t like it, i don’t like it, but if some day somehow i happens to be that beastly person, i wish it only resides in computer security category. 6. I like bread because bread TALK but people usually don’t.
ruby's "python -m SimpleHTTPServer"
If I remembered right, the command below used to be the most voted one on commandlinefu.com: [cc lang=”bash”] $ python -m SimpleHTTPServer [/cc] the “SimpleHTTPServer” module is written in python and it maybe called “the python way”, but that’s not our topic today. what we really want to do here is just making a ruby equivalent for this popular command, using rack. first, make sure you have rack gem installed on your system (if you’ve installed rails 2.x before, you may already have it): [cc lang=”bash”] $ sudo gem install rack [/cc] confirm installation (Rack 1.0 should also work for our example): [cc lang=”bash”] $ rackup –version Rack 1.1 [/cc] alright, preparation is done, let’s start cooking. only a rackup file will be sufficient, for consistency, let’s just call it simple_http_server.ru . [cc lang=”ruby”] #!/usr/bin/env rackup #\ -E deployment use Rack::ContentLength app = Rack::Directory.new Dir.pwd run app [/cc] Rack::Directory is so awesome that we just simply rely on it… aren’t i cheating? hehe, maybe… ok, we’re done. to test it, simply run: [cc lang=”bash”] $ rackup simple_http_server.ru [/cc] and then point your browser to http://localhost:9292/ . to be useful for daily use, you may consider chmod +x it to make it executable and put it in your $PATH. [cc lang=”bash”] $ chmod +x simple_http_server.ru $ export PATH=$PATH:`pwd` [/cc] to share a directory with your coworkers or friends, simply cd to that directory and execute the script: [cc lang=”bash”] $ cd /the/dir/i/want/to/share $ simple_http_server.ru [/cc] since we’re actually using rackup to execute, all the rackup options are also available, which is good: [cc lang=”bash”] $ simple_http_server.ru -h [/cc] finally, let’s end up with emulating python SimpleHTTPServer’s default behavior: [cc lang=”bash”] $ simple_http_server.ru -p 8000 [/cc]
pvz on iphone/itouch, now is the time
pvz on itunes ps: jailbreak == DO NOT upgrade at the first time, be careful when itunes prompts you with a new firmware version (3.1.3) available dialog.
All right
alright, all right… happy chinese new year and happy valentine’s day~ although they don’t have chinese new year, they do have the 2nd one, hehe, so… well, not bad news at least, xd btw, what’s more important to them, work or love? hard to tell? alright, all right, let’s see~
Mobile Number Changed
for specific details, please check my msn signature, thx. if you happens to know me and want to add me as your friend on msn, please check my contact info page.
奶奶,一路顺风
手机屏幕上熟悉而又陌生的号码突然闪现,一股不祥之兆油然而生 放下电话后便是无限的震惊和愧疚…… 也许是天意,我没能见到奶奶最后一面 但电话那头的我,却不知道为啥刻意表现的格外镇定 也许是我知道这一刻迟早会来吧 但这一刻是来的如此之快,如此突然…… 奶奶就这么匆忙地回故乡了 带着我厚厚一叠童年的回忆上路了 没留给我任何送行的时间 却留给我永远的遗憾…… 而这一切,本都应该十分完美…… 也许这是长大后必然将要慢慢面临的事情吧 看着周围最最亲近的人都渐渐离你远去,却这么的无能为力 只能独自慢慢学会更加坚强…… 好了,不矫情了,不就是回故乡吗 奶奶路上小心了,一路顺风 我会常常想你的……
Update-28
今天简短一点,短小而不精悍…… 1. 有种修辞叫明褒实贬,现在澄清一下update 27#1便是采用此种修辞手法,原因很简单,我感冒了…… 2. 塞翁失马,焉知非福(鄙人不以为然,但是姜太公钓鱼,那啥那啥…),献给最近祸不单行的ivan兄~ 3. tweetie竟然不支持修改refresh interval…… 唉,最近老是在疯掉和忍住不疯之间徘徊…… 4. 电驴拖24,第二集比第一集快,第四集比第三集快,反正各种杯具餐具应有尽有,令人目不暇接 5. 经济危机只是幌子,该发生的总会发生,不该发生的咱会编个理由让它发生 6. 欲加之罪,何患无辞,昨日闻某噩耗有感 7. sunng,快来看凄凉~
Update-27
1. 据说下周气温能涨到20多度,魔都果然是魔都,哪天温室效应到人工四季如春我就更“喜欢”了…… 不过好歹也算是近来为数不多的值得令人高兴的事情了,可惜目前还只是据说而已,着实讽刺…… 2. google.cn…… 唉…… 佛曰:不可说,不可说,一说即是错,这儿我就不错了,你我心照不宣,慈悲为怀,阿弥陀佛…… 3. 24小时第8季美国时间今日回归,据说(怎么又是据说…汗…)连续两天会播放四集,有点小激动,一把年纪了这么激动也不容易的,心脏病出来就麻烦了,所以淡定,恩,淡定… 我很淡定…… 3分钟后…… 唉,其实我就关心tony almeida是好人是坏人,编剧表这样纠结啦…… 没意思哒…… 4. “if you have a hammer everything looks like a nail”,这个道理是普世的,并不局限于软件开发,不知道我在说啥的我就不浪费感情了,前几天空等谣言的cnn直播goolge官方回应已经浪费了我太多的感情,我还要自己reserve一点留作它用的…… 用很多人喜欢的xiaolai兄的twitter留言总结一下就是“人们太过喜欢一成不变的东西了——因为不确定是最可怕的事情” BTW,xiaolai兄着实imba,一有人follow上去,他基本上就会follow回来,看看他following的人数我就想他丫果然牛逼,这阅读量…这阅读速度… 一个字,imba;两个字,很imba,三个字,我不说了,大家以此类推…… 5. 以前就知道国内四大门户流行自我阉割,现在终于明白原来这在我们伟大的那啥也是普世真理…… 希望下次开心网删我记录的时候提前通知我下,发我一封病危通知书,好让家属我有个心理准备,这个要求还算合理吧…… PS:现在武林乱了,葵花宝典,辟邪剑谱啥的都不稀奇的,那么寒碜的东西怎么还拿的出手?到这儿来只会丢人现眼的…… 欲练此功,必先自宫?呵呵,拉倒吧…… 6. 心情郁闷的码着本文的时候,twitter上突然跳出来的,“2009IT企业前台MM选美大赛”,看上去蛮有趣的样子,于是上去惊鸿一瞥了一下,咦,怎么没有鄙人公司的前台mm呢?纳闷,烦闷,然后继续郁闷的码字…… 7. md就这样了,不码了,加班就是这样让人心情舒畅,一周不加它个一晚两晚就是他娘的不爽……
好久不见
最近喜欢听翻唱的,总有不一样的感觉. media downloaded from google.cn music,赶在google.cn还没有离去之前…… 蔡依林-好久不见 我来到 你的城市 走过你来时的路 想像着 没我的日子 你是怎样的孤独 拿着你 给的照片 熟悉的那一条街 只是没了你的画面 我们回不到那天 你会不会忽然的出现 在街角的咖啡店 我会带着笑脸 挥手寒暄 和你 坐着聊聊天 我多么想和你见一面 看看你最近改变 不再去说从前 只是寒暄 对你说一句 只是说一句 好久不见 拿着你 给的照片 熟悉的那一条街 只是没了你的画面 我们回不到那天 你会不会忽然的出现 在街角的咖啡店 我会带着笑脸 挥手寒暄 和你 坐着聊聊天 我多么想和你见一面 看看你最近改变 不再去说从前 只是寒暄 对你说一句 只是说一句 好久不见