WHITELEAF:Kindle応援サイト

KindleでWEB小説を読もう! Narou.rb 公開中

Rubyベンチ

push or <<

Array に要素を挿入するメソッドは push ですが、 require "benchmark" Benchmark.bm { |bm| bm.report("push") { a = [] 5000000.times { |i| a.push(i) } } bm.report(" << ") { a = [] 5000000.times { |i| a << i } } } ruby 1.9.2dev (2010-04-27 trunk…

:"symbol"

気になったので他のパターンも。JRuby のための計測です。 require "benchmark" def test_symbol(sym) a = sym end Benchmark.bm { |bm| bm.report("(1)") { 10000000.times { test_symbol(:abc) } } bm.report("(2)") { 10000000.times { test_symbol(:"abc…

access hash

Hash をアクセスする際に Fixnum, Symbol, String を使う場合の比較です。Hash ではなく Array を使った結果もつけておきます。 require "benchmark" class Test def initialize @num_table = {} @sym_table = {} @str_table = {} @array = [] 100.times { |…

Server VM でも試してみました。オプションはよくわからないのでかたっぱしから速くなりそうなのを……。--fast はエラーが出てダメでした。 >jruby --server -v -J-Djruby.compile.frameless=true -J-Djruby.compile.position less=true -J-Djruby.compile.th…

ダブルクォーテーション or シングルクォーテーション

require "benchmark" Benchmark.bm { |bm| bm.report("double") { 20000000.times { a = "strings" } } bm.report("single") { 20000000.times { a = 'strings' } } } ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32] user system total real double…

1liner?

require "benchmark" def f1 return false || true end def f2 true unless false return false end Benchmark.bm { |bm| bm.report("f1") { 20000000.times { f1 } } bm.report("f2") { 20000000.times { f2 } } } ruby 1.8.6 (2008-08-11 patchlevel 287) …

少しでも速くするために

こまか〜いことでも、意外と速度に響いてくることがよくあります。響かないときもあります。思いついたときに実際にベンチーマークを取ったものをこまめにメモっておくことにします。[Rubyベンチ]タグをつけておいて分かりやすいようにしておこう。