Rubyã®æååã®é£çµé度ã測å®ãã¦Gruffã§è¡¨ç¤ºãã¦ã¿ã
String と StringBuffer の文字列連結速度のテスト - すえひろがりっっっっ!ã¨åæ§å°ãã¿ã
社ä¼äººã«ãªã£ã¦1å¹´ãã¡ã¾ãããä»äºã§ãç´ äººããï¼ãã£ã¦æãã³ã¼ããããè¦ããã¾ãã
ãã®ããã10ã«å
¥ãã®ããJavaã§+= ã§æååãé£çµãã¦ãã¦ããã©ã¼ãã³ã¹ãããã¶ãæªããã³ã¼ãã§ãã
ããããã£ã¦ã人ã¯ããã¾ã ã« += ã§æååãé£çµãã¦ããã³ã¼ãããããã¦ãã¾ãã
ã©ãã«ããªããªãããã ãããã
ã¨ããã§ãRuby ã§ã String ã®é£çµã¯éãã®ã ããï¼ã¨ãµã¨æã£ãã®ã§ += 㨠<< ã§çµåããå ´åã®é度ã測å®ãã¦ã¿ã¾ããã
楽ããRubyã«ãã㨠Ruby ã§ã += ã§çµåããã¨ã¤ã³ã¹ã¿ã³ã¹ãçæãããããã
ã¨ãããã¨ã¯ += 㨠<< ã§ã¯æ§è½ã«å·®ãåºãã¯ãã
ãã£ããRubyãªã®ã§Gruffã試ãã¦ã¿ã¾ãã
ãã¼ã¸ã§ã³
Mac OS X 10.6.3
CPU 3.06 GHz Intel Core 2 Duo
ã¡ã¢ãª 8GB
ruby 1.8.7
gruff 0.3.6
å®é¨ã³ã¼ã
require 'rubygems' require 'gruff' require 'benchmark' # æ大çµååæ° MAX = 100000 # å»ã¿ STEP = 1000 g = Gruff::Line.new # æå®ããããæ¹ã§ 0 ã 10ä¸åã®æååçµåã«ãããæéãè¨æ¸¬ãã # è¿ãå¤ã¯è¨æ¸¬ããæéã®é å def bench benchmarks = [] 0.step(MAX, STEP) do |i| benchmarks << Benchmark.measure { str = '' i.times do str = yield str end } end benchmarks.collect{|b| b.real} end g.theme_37signals # += ã§çµå g.data("+=", bench{|str| str += 'a'}) # concat ã§çµå g.data("concat", bench{|str| str.concat('a')}) # << ã§çµå g.data("<<", bench{|str| str << 'a'}) 0.step(MAX, MAX / 4) do |i| g.labels.merge!({i/STEP => i.to_s}) end g.x_axis_label = 'Times' g.y_axis_label = 'seconds' g.write('result.png')
- "+=" 㨠"concat" 㨠"<<" ã§çµåããå ´åãè¨æ¸¬ãã¦ãã¾ãã
- è¨æ¸¬æ¹æ³ãå¤ããæå®ããã®ã«é«éé¢æ°ã«ãããã£ãã®ã ãã©ãRuby ã¯ãã㧠yield 使ã£ã¦ãããã¯ã渡ãã®ã ã£ããã¨ãããã¨ã§ Rubyäºå¹´ç®ã«ãã¦å yieldãæ¸ãæ¹ãã£ã¦ãã ãããããã
- 0ã10ä¸åã¾ã§ãã¹ã¦è©¦ãã¨æéãããããããã®ã§ 1000åå»ã¿ã«ãã¦ã¾ããããã§ãçµæ§ãããã
çµæ
ãããªæããGruffã§æç»ããã°ã©ãã¯ç¶ºéºã ãï¼
横軸ãçµåã®åæ°ã縦軸ãããã£ãæé(ç§)ã§ãã
"<<"ã®ç·è²ã¯ã»ã¼é ãã¦ãã¾ã£ã¦ãã¾ããã"concat"ã®éè²ã«ä¸æ¸ãããã¦ãã¾ãorz
ãã£ã±ã += ã¯å§åçã«é
ãã
ãã£ã¨Javaã¨åæ§ãã¤ã³ã¹ã¿ã³ã¹çæåã®å·®ã§ããã
çµè«
- ãããã "+=" ãããã¨ãã¯ã"<<"ã使ãããã«ãããã
- Gruff ã¯ã°ã©ãã¼ã·ã§ã³ãããã綺éºãªã°ã©ããç°¡åã«æãã