Created
May 17, 2009 08:39
-
-
Save thinkerbot/112965 to your computer and use it in GitHub Desktop.
ruby core benchmarks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*.rbc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
# Benchmarks for various array operations. | |
# | |
# user system total real | |
# Array.new 0.570000 0.000000 0.570000 ( 0.572061) | |
# [] 0.380000 0.000000 0.380000 ( 0.383014) | |
# dup 0.560000 0.000000 0.560000 ( 0.554559) | |
# replace 0.410000 0.000000 0.410000 ( 0.411269) | |
# pack * 1.670000 0.000000 1.670000 ( 1.670611) | |
# pack H40 1.740000 0.000000 1.740000 ( 1.752113) | |
# pack H40*10 1.180000 0.000000 1.180000 ( 1.179426) | |
# | |
Benchmark.bm(20) do |x| | |
n = 1000000 | |
a = [1,2,3] | |
b = [4,5,6] | |
x.report("Array.new") do | |
n.times do | |
Array.new | |
end | |
end | |
x.report("[]") do | |
n.times do | |
[] | |
end | |
end | |
x.report("dup") do | |
n.times do | |
a.dup | |
end | |
end | |
x.report("replace") do | |
n.times do | |
a.replace(b) | |
end | |
end | |
x.report("pack *") do | |
sha = "ee8b5c4ddb843673ef659cfebd0c9e4cbe8ca59d" | |
a = [sha] | |
n.times { a.pack("H*") } | |
end | |
x.report("pack H40") do | |
sha = "ee8b5c4ddb843673ef659cfebd0c9e4cbe8ca59d" | |
a = [sha] | |
n.times { a.pack("H40") } | |
end | |
x.report("pack H40*10") do | |
sha = "ee8b5c4ddb843673ef659cfebd0c9e4cbe8ca59d" | |
a = Array.new(10, sha) | |
(n/10).times { a.pack("H40" * 10) } | |
end | |
end | |
# An interesting thing to note here is how the time for unshift 'exponentially' | |
# increases with n. The other methods are more linear in time. Note too that | |
# shift is faster than pop; ie a FIFO queue using push/shift is fastest. | |
# | |
# user system total real | |
# push 10k 0.010000 0.000000 0.010000 ( 0.002962) | |
# pop 10k 0.000000 0.000000 0.000000 ( 0.003004) | |
# unshift 10k 0.040000 0.000000 0.040000 ( 0.041590) | |
# shift 10k 0.000000 0.000000 0.000000 ( 0.002379) | |
# user system total real | |
# push 100k 0.030000 0.000000 0.030000 ( 0.027808) | |
# pop 100k 0.030000 0.000000 0.030000 ( 0.025829) | |
# unshift 100k 4.130000 0.020000 4.150000 ( 4.223530) | |
# shift 100k 0.020000 0.000000 0.020000 ( 0.023153) | |
# user system total real | |
# push 1000k 0.280000 0.010000 0.290000 ( 0.278975) | |
# pop 1000k 0.250000 0.000000 0.250000 ( 0.253572) | |
# shift 1000k 0.220000 0.000000 0.220000 ( 0.219806) | |
# [1000] 1000k 0.240000 0.000000 0.240000 ( 0.249434) | |
# at(1000) 1000k 0.250000 0.000000 0.250000 ( 0.242768) | |
# | |
[10, 100, 1000].each do |n| | |
Benchmark.bm(20) do |x| | |
m = n * 1000 | |
array = [] | |
x.report("push #{n}k") do | |
m.times { array.push(nil) } | |
end | |
x.report("pop #{n}k") do | |
m.times { array.pop } | |
end | |
x.report("unshift #{n}k") do | |
m.times { array.unshift(nil) } | |
end unless n > 100 | |
x.report("shift #{n}k") do | |
m.times { array.shift } | |
end | |
x.report("[1000] #{n}k") do | |
m.times { array[1000] } | |
end | |
x.report("at(1000) #{n}k") do | |
m.times { array.at(1000) } | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
require 'enumerator' | |
# | |
# Benchmarks for various miscellaneous operations. | |
# | |
# $ ruby enum_for_benchmark.rb | |
# user system total real | |
# 10k each 0.700000 0.000000 0.700000 ( 0.700964) | |
# 10k each on enum 2.040000 0.000000 2.040000 ( 2.039822) | |
# 10k to_enum.each 2.050000 0.000000 2.050000 ( 2.050050) | |
Benchmark.bm(25) do |x| | |
m = 10 | |
n = 1000 * m | |
array = Array.new(1000) | |
x.report "#{m}k each" do | |
n.times { array.each {|e| } } | |
end | |
x.report "#{m}k each on enum" do | |
enum = array.to_enum(:each) | |
n.times { enum.each {|e| } } | |
end | |
x.report "#{m}k to_enum.each" do | |
n.times { array.to_enum(:each).each {|e| } } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
# Benchmarks for various File operations. | |
# | |
# user system total real | |
# expand_path . 0.040000 0.110000 0.150000 ( 0.160505) | |
# expand_path ./../dir 0.050000 0.110000 0.160000 ( 0.156961) | |
# expand_path ~ 0.010000 0.000000 0.010000 ( 0.008672) | |
# expand_path ~/../dir 0.010000 0.000000 0.010000 ( 0.009154) | |
# expand_path / 0.010000 0.000000 0.010000 ( 0.007646) | |
# expand_path /path/to/dir 0.000000 0.000000 0.000000 ( 0.007777) | |
# mtime 0.030000 0.020000 0.050000 ( 0.044722) | |
# | |
Benchmark.bm(25) do |x| | |
n = 10000 | |
x.report "expand_path ." do | |
n.times { File.expand_path(".") } | |
end | |
x.report "expand_path ./../dir" do | |
n.times { File.expand_path("./../dir") } | |
end | |
x.report "expand_path ~" do | |
n.times { File.expand_path("~") } | |
end | |
x.report "expand_path ~/../dir" do | |
n.times { File.expand_path("~/../dir") } | |
end | |
x.report "expand_path /" do | |
n.times { File.expand_path("/") } | |
end | |
x.report "expand_path /path/to/dir" do | |
n.times { File.expand_path("/path/to/dir") } | |
end | |
x.report "mtime" do | |
n.times { File.mtime __FILE__ } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
# Benchmarks for variations of hash operations. | |
# | |
# user system total real | |
# each_pair 1.390000 0.000000 1.390000 ( 1.393821) | |
# each_key 0.440000 0.000000 0.440000 ( 0.436525) | |
# each_value 0.440000 0.000000 0.440000 ( 0.439302) | |
# keys.each 0.530000 0.000000 0.530000 ( 0.532249) | |
# values.each 0.530000 0.000000 0.530000 ( 0.536871) | |
# [:k]=:v (size=1) 0.030000 0.000000 0.030000 ( 0.030153) | |
# [:k] (size=1) 0.030000 0.000000 0.030000 ( 0.027243) | |
# | |
Benchmark.bm(20) do |x| | |
n = 100000 | |
hash = {} | |
('a'..'z').to_a.each do |letter| | |
hash[letter] = letter.upcase | |
end | |
x.report "each_pair" do | |
n.times { hash.each_pair {|key, value|} } | |
end | |
x.report "each_key" do | |
n.times { hash.each_key {|key|} } | |
end | |
x.report "each_value" do | |
n.times { hash.each_value {|value|} } | |
end | |
x.report "keys.each" do | |
n.times { hash.keys.each {|key|} } | |
end | |
x.report "values.each" do | |
n.times { hash.values.each {|value|} } | |
end | |
x.report("each") do | |
n.times { hash.each {|entry| } } | |
end | |
array = hash.to_a | |
x.report("to_a.each") do | |
n.times { array.each {|entry| } } | |
end | |
hash = {:key => :value} | |
x.report "[:k]=:v (size=1)" do | |
n.times { hash[:key] = :value } | |
end | |
x.report "[:k] (size=1)" do | |
n.times { hash[:key] } | |
end | |
x.report("clear") do | |
n.times { hash.clear } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
# Benchmarks for IO/File | |
# | |
# user system total real | |
# reference 0.000000 0.000000 0.000000 ( 0.001515) | |
# IO.open(0) 0.170000 0.020000 0.190000 ( 0.193146) | |
# IO.open(2) 0.470000 0.020000 0.490000 ( 0.487771) | |
# File.open 0.820000 0.110000 0.930000 ( 0.930175) | |
# | |
Benchmark.bm(20) do |x| | |
n = 10000 | |
x.report("reference") do | |
n.times { $stdout } | |
end | |
x.report("IO.open(0)") do | |
n.times { IO.open(1) } | |
end | |
x.report("IO.open(1)") do | |
n.times { IO.open(1) } | |
end | |
x.report("File.open") do | |
n.times { File.open(__FILE__) {} } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
# Benchmarks for various ways of calling methods. | |
# user system total real | |
# object_id 0.070000 0.000000 0.070000 ( 0.070565) | |
# send(:object_id) 0.110000 0.000000 0.110000 ( 0.110025) | |
# send('object_id') 0.240000 0.000000 0.240000 ( 0.236216) | |
# call 0.110000 0.000000 0.110000 ( 0.108683) | |
# Delegate 0.110000 0.000000 0.110000 ( 0.104668) | |
Benchmark.bm(25) do |x| | |
n = 1000000 | |
obj = Object.new | |
1000.times { obj.object_id } | |
x.report "object_id" do | |
n.times { obj.object_id } | |
end | |
1000.times { Object.send(:object_id) } | |
x.report "send(:object_id)" do | |
n.times { Object.send(:object_id) } | |
end | |
1000.times { Object.send('object_id') } | |
x.report "send('object_id')" do | |
n.times { Object.send('object_id') } | |
end | |
obj_id_method = obj.method(:object_id) | |
1000.times { obj_id_method.call } | |
x.report "call" do | |
n.times { obj_id_method.call } | |
end | |
class Delegate | |
attr_reader :obj | |
def initialize(obj) | |
@obj = obj | |
end | |
def call | |
obj.object_id | |
end | |
end | |
delegate = Delegate.new(obj) | |
1000.times { delegate.call } | |
x.report "Delegate" do | |
n.times { delegate.call } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
# | |
# Benchmarks for various miscellaneous operations. | |
# | |
# This set of benchmarks looks at two ways of constructing a logger: | |
# with an interpolated string that gets operated on given a flag, | |
# or a block that yields the interpolated string, given a flag. | |
# | |
# user system total real | |
# ** short strings ** | |
# m("string", true) 0.100000 0.000000 0.100000 ( 0.105140) | |
# m("string", false) 0.100000 0.000000 0.100000 ( 0.103092) | |
# m(true) # :yield: 0.150000 0.000000 0.150000 ( 0.147326) | |
# m(false) 0.050000 0.000000 0.050000 ( 0.047492) | |
# | |
# ** long strings ** | |
# m("string", true) 0.260000 0.010000 0.270000 ( 0.265709) | |
# m("string", false) 0.260000 0.000000 0.260000 ( 0.265424) | |
# m(true) # :yield: 0.290000 0.010000 0.300000 ( 0.296475) | |
# m(false) 0.050000 0.000000 0.050000 ( 0.048064) | |
# | |
# ** assignment from array ** | |
# a, b = *array 0.070000 0.000000 0.070000 ( 0.069233) | |
# a, *b = array 0.090000 0.000000 0.090000 ( 0.094843) | |
# a, b = (shift, shift) 0.130000 0.000000 0.130000 ( 0.124958) | |
# a = (shift); b = (shift) 0.090000 0.000000 0.090000 ( 0.091782) | |
# a, b, c, d, e = *array 0.090000 0.000000 0.090000 ( 0.094531) | |
# a = (shift); ... 0.170000 0.000000 0.170000 ( 0.165836) | |
# | |
# ** various operations ** | |
# !!obj 0.190000 0.000000 0.190000 ( 0.192186) | |
# obj ? true : false 0.160000 0.000000 0.160000 ( 0.165349) | |
# if obj then else end 0.170000 0.000000 0.170000 ( 0.171080) | |
# | |
# ** method signatures ** | |
# method call 0.320000 0.000000 0.320000 ( 0.320484) | |
# method with block 0.400000 0.000000 0.400000 ( 0.397699) | |
# method with &block 0.490000 0.000000 0.490000 ( 0.483668) | |
# bloc call 0.360000 0.000000 0.360000 ( 0.361628) | |
# bloc with block 3.310000 0.770000 4.080000 ( 4.078993) | |
# bloc with &block 0.530000 0.000000 0.530000 ( 0.531641) | |
# with arg 0.370000 0.000000 0.370000 ( 0.369745) | |
# with arg and block 0.440000 0.000000 0.440000 ( 0.434652) | |
# with arg and &block 0.550000 0.000000 0.550000 ( 0.553375) | |
# splat 0.570000 0.000000 0.570000 ( 0.578798) | |
# splat with arg 0.600000 0.000000 0.600000 ( 0.597309) | |
# | |
# ** definition method ** | |
# mod_def (on module) 0.350000 0.000000 0.350000 ( 0.352461) | |
# standard_def 0.320000 0.000000 0.320000 ( 0.318882) | |
# block_def 0.690000 0.000000 0.690000 ( 0.685312) | |
# lambda_def 0.680000 0.000000 0.680000 ( 0.680720) | |
# eval_def 0.330000 0.000000 0.330000 ( 0.330150) | |
# mod_def 1.090000 0.000000 1.090000 ( 1.087596) | |
# obj_def 1.080000 0.000000 1.080000 ( 1.082260) | |
# | |
# Note it is very expensive to turn a block into a proc | |
Benchmark.bm(25) do |x| | |
puts "** short strings **" | |
n = 100000 | |
str = "interpolation" | |
def m_str(input, check) | |
check ? true : false | |
end | |
x.report "m(\"string\", true)" do | |
n.times { m_str("string #{str}", true) } | |
end | |
x.report "m(\"string\", false)" do | |
n.times { m_str("string #{str}", false) } | |
end | |
def m_yield(check) | |
check ? yield : false | |
end | |
x.report "m(true) # :yield: " do | |
n.times { m_yield(true) { "string #{str}" } } | |
end | |
x.report "m(false)" do | |
n.times { m_yield(false) { "string #{str}" } } | |
end | |
puts | |
puts "** long strings **" | |
x.report "m(\"string\", true)" do | |
n.times { m_str("string #{str} string #{str} string #{str} string #{str} string #{str}", true) } | |
end | |
x.report "m(\"string\", false)" do | |
n.times { m_str("string #{str} string #{str} string #{str} string #{str} string #{str}", false) } | |
end | |
x.report "m(true) # :yield: " do | |
n.times { m_yield(true) { "string #{str} string #{str} string #{str} string #{str} string #{str}" } } | |
end | |
x.report "m(false)" do | |
n.times { m_yield(false) { "string #{str} string #{str} string #{str} string #{str} string #{str}" } } | |
end | |
puts | |
puts "** assignment from array **" | |
x.report "a, b = *array" do | |
n.times { | |
array = [1,2,3] | |
a, b = *array | |
} | |
end | |
x.report "a, *b = array" do | |
n.times { | |
array = [1,2,3] | |
a, *b = array | |
} | |
end | |
x.report "a, b = (shift, shift)" do | |
n.times { | |
array = [1,2,3] | |
a, b = array.shift, array.shift | |
} | |
end | |
x.report "a = (shift); b = (shift)" do | |
n.times { | |
array = [1,2,3] | |
a = array.shift | |
b = array.shift | |
} | |
end | |
x.report "a, b, c, d, e = *array" do | |
n.times { | |
array = [1,2,3,4,5] | |
a, b, c, d, e = *array | |
} | |
end | |
x.report "a = (shift); ..." do | |
n.times { | |
array = [1,2,3,4,5] | |
a = array.shift | |
b = array.shift | |
c = array.shift | |
d = array.shift | |
e = array.shift | |
} | |
end | |
puts | |
puts "** various operations **" | |
x.report "!!obj" do | |
obj = Object.new | |
(10 * n).times { !!obj } | |
end | |
x.report "obj ? true : false" do | |
obj = Object.new | |
(10 * n).times { obj ? true : false } | |
end | |
x.report "if obj then else end" do | |
obj = Object.new | |
(10 * n).times { if obj then true else false end } | |
end | |
puts | |
puts "** method signatures **" | |
class Sample | |
def meth | |
end | |
def args(obj) | |
end | |
def bloc(&block) | |
end | |
def yeeld() | |
yield | |
end | |
def splat(*args) | |
end | |
end | |
x.report "method call" do | |
obj = Sample.new | |
(10 * n).times { obj.meth } | |
end | |
x.report "method with block" do | |
obj = Sample.new | |
(10 * n).times { obj.meth {} } | |
end | |
x.report "method with &block" do | |
obj = Sample.new | |
block = lambda {} | |
(10 * n).times { obj.meth(&block) } | |
end | |
x.report "bloc call" do | |
obj = Sample.new | |
(10 * n).times { obj.bloc } | |
end | |
x.report "bloc with block" do | |
obj = Sample.new | |
(10 * n).times { obj.bloc {} } | |
end | |
x.report "bloc with &block" do | |
obj = Sample.new | |
block = lambda {} | |
(10 * n).times { obj.bloc(&block) } | |
end | |
x.report "method with yield" do | |
obj = Sample.new | |
(10 * n).times { obj.yeeld {} } | |
end | |
x.report "method with yield &block" do | |
obj = Sample.new | |
block = lambda {} | |
(10 * n).times { obj.yeeld(&block) } | |
end | |
x.report "with arg" do | |
obj = Sample.new | |
(10 * n).times { obj.args(nil) } | |
end | |
x.report "with arg and block" do | |
obj = Sample.new | |
(10 * n).times { obj.args(nil) {} } | |
end | |
x.report "with arg and &block" do | |
obj = Sample.new | |
block = lambda {} | |
(10 * n).times { obj.args(nil, &block) } | |
end | |
x.report "splat" do | |
obj = Sample.new | |
(10 * n).times { obj.splat } | |
end | |
x.report "splat with arg" do | |
obj = Sample.new | |
(10 * n).times { obj.splat(nil) } | |
end | |
puts | |
puts "** definition method **" | |
module MethodDefMod | |
module_function | |
def mod_def | |
true | |
end | |
end | |
class ObjectDefMethod | |
def obj_def | |
true | |
end | |
end | |
class MethodDef | |
def standard_def | |
true | |
end | |
define_method :block_def do | |
true | |
end | |
block = lambda do | |
true | |
end | |
define_method :lambda_def, &block | |
define_method :mod_def, &MethodDefMod.method(:mod_def) | |
define_method :obj_def, &ObjectDefMethod.new.method(:obj_def) | |
end | |
MethodDef.class_eval %q{ | |
def eval_def | |
true | |
end | |
} | |
n = 1000 * 1000 | |
obj = MethodDef.new | |
x.report "mod_def (on module)" do | |
n.times { MethodDefMod.mod_def } | |
end | |
x.report "standard_def" do | |
n.times { obj.standard_def } | |
end | |
x.report "block_def" do | |
n.times { obj.block_def } | |
end | |
x.report "lambda_def" do | |
n.times { obj.lambda_def } | |
end | |
x.report "eval_def" do | |
n.times { obj.eval_def } | |
end | |
x.report "mod_def" do | |
n.times { obj.mod_def } | |
end | |
x.report "obj_def" do | |
n.times { obj.obj_def } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
module A | |
end | |
class B | |
include A | |
end | |
class C < B | |
end | |
# Benchmarks for various module operations. | |
# | |
# user system total real | |
# object_id (control) 0.230000 0.000000 0.230000 ( 0.234879) | |
# [] (control) 0.380000 0.000000 0.380000 ( 0.379503) | |
# ancestors C<B.A 0.650000 0.000000 0.650000 ( 0.658281) | |
# ancestors B.A 0.610000 0.010000 0.620000 ( 0.623774) | |
# ancestors A 0.530000 0.000000 0.530000 ( 0.542385) | |
# superclass C<B.A 0.240000 0.000000 0.240000 ( 0.241200) | |
# superclass B.A 0.240000 0.000000 0.240000 ( 0.249191) | |
# inc_mod C<B.A 0.570000 0.010000 0.580000 ( 0.573147) | |
# inc_mod B.A 0.550000 0.000000 0.550000 ( 0.564256) | |
# inc_mod A 0.520000 0.000000 0.520000 ( 0.523128) | |
# C.kind_of?(A) 0.310000 0.000000 0.310000 ( 0.308133) | |
# B.kind_of?(A) 0.310000 0.000000 0.310000 ( 0.308827) | |
# A.kind_of?(A) 0.300000 0.000000 0.300000 ( 0.299264) | |
# | |
# Much of the time required for ancestors appears associated with the | |
# creation of an array. | |
Benchmark.bm(20) do |x| | |
n = 1000000 | |
x.report "object_id (control)" do | |
n.times { C.object_id } | |
end | |
x.report "[] (control)" do | |
n.times { [] } | |
end | |
x.report "ancestors C<B.A" do | |
n.times { C.ancestors } | |
end | |
x.report "ancestors B.A" do | |
n.times { B.ancestors } | |
end | |
x.report "ancestors A" do | |
n.times { A.ancestors } | |
end | |
x.report "superclass C<B.A" do | |
n.times { C.superclass } | |
end | |
x.report "superclass B.A" do | |
n.times { B.superclass } | |
end | |
x.report "inc_mod C<B.A" do | |
n.times { C.included_modules } | |
end | |
x.report "inc_mod B.A" do | |
n.times { B.included_modules } | |
end | |
x.report "inc_mod A" do | |
n.times { A.included_modules } | |
end | |
x.report "C.kind_of?(A)" do | |
n.times { C.kind_of?(A) } | |
end | |
x.report "B.kind_of?(A)" do | |
n.times { B.kind_of?(A) } | |
end | |
x.report "A.kind_of?(A)" do | |
n.times { A.kind_of?(A) } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
# Benchmarks for various object operations. | |
# | |
# user system total real | |
# respond_to? 0.300000 0.000000 0.300000 ( 0.301135) | |
# object_id 0.220000 0.000000 0.220000 ( 0.221668) | |
# == nil 0.240000 0.000000 0.240000 ( 0.244954) | |
# nil? 0.220000 0.000000 0.220000 ( 0.221454) | |
# | |
Benchmark.bm(20) do |x| | |
n = 1000000 | |
o = Object.new | |
x.report "respond_to?" do | |
n.times { o.respond_to?(:object_id) } | |
end | |
x.report "object_id" do | |
n.times { o.object_id } | |
end | |
x.report "== nil" do | |
n.times { o == nil } | |
end | |
x.report "nil?" do | |
n.times { o.nil? } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
# Benchmarks for various ObjectSpace operations. | |
# | |
# user system total real | |
# Object::Const 0.220000 0.000000 0.220000 ( 0.221523) | |
# const_get 0.320000 0.000000 0.320000 ( 0.318768) | |
# _id2ref 0.290000 0.000000 0.290000 ( 0.292355) | |
# _id2ref hex 0.300000 0.000000 0.300000 ( 0.294529) | |
# send _id2ref 0.430000 0.000000 0.430000 ( 0.431114) | |
# | |
# The nice thing to notice here is that _id2ref is very quick. | |
Benchmark.bm(20) do |x| | |
n = 1000000 | |
id = Object.new.object_id | |
x.report "Object::Const" do | |
n.times { Object::ObjectSpace } | |
end | |
x.report "const_get" do | |
n.times { Object.const_get(:ObjectSpace) } | |
end | |
x.report "_id2ref" do | |
n.times { ObjectSpace._id2ref(id) } | |
end | |
hex = id.to_s(16) | |
x.report "_id2ref hex" do | |
n.times { ObjectSpace._id2ref(id) } | |
end | |
x.report "send _id2ref" do | |
n.times { ObjectSpace.send(:_id2ref, id) } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
# Benchmarks for various regexps. | |
# | |
# user system total real | |
# str[0] = ?a 0.400000 0.000000 0.400000 ( 0.421058) | |
# str.rindex('a') == 0 0.550000 0.000000 0.550000 ( 0.567045) | |
# str =~ /\Aa/ 0.670000 0.000000 0.670000 ( 0.687583) | |
# | |
# user system total real | |
# 100k constant 0.090000 0.000000 0.090000 ( 0.096235) | |
# 100k static 0.070000 0.000000 0.070000 ( 0.074747) | |
# 100k dynamic 0.490000 0.020000 0.510000 ( 0.503871) | |
Benchmark.bm(20) do |x| | |
n = 1000000 | |
str = "abcde" | |
x.report "str[0] = ?a" do | |
n.times { str[0] == ?a } | |
end | |
x.report "str.rindex('a') == 0" do | |
n.times { str.rindex('a') == 0 } | |
end | |
re = /\Aa/ | |
x.report "str =~ /\\Aa/" do | |
n.times { str =~ re } | |
end | |
str = '--a' | |
pos = /\A--?\w/ | |
x.report "pos /\\A--?\\w/" do | |
n.times { str =~ pos } | |
end | |
neg = /\A-(?!-?\w)/ | |
x.report "neg /\\A-(?!-?\\w)/" do | |
n.times { str =~ neg } | |
end | |
end | |
Benchmark.bm(20) do |x| | |
n = 100 | |
str = "abc" | |
REGEXP = /abc/ | |
x.report "#{n}k constant" do | |
(n*1000).times { str =~ REGEXP } | |
end | |
x.report "#{n}k static" do | |
(n*1000).times { str =~ /abc/ } | |
end | |
x.report "#{n}k dynamic" do | |
(n*1000).times { str =~ /#{str}/ } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
# Benchmarks for various string operations. | |
# | |
# user system total real | |
# a 0.130000 0.000000 0.130000 ( 0.131640) | |
# a to_s 0.230000 0.000000 0.230000 ( 0.227468) | |
# a to_sym 0.260000 0.000000 0.260000 ( 0.262291) | |
# a..z to_sym 0.310000 0.000000 0.310000 ( 0.316368) | |
# {}[str] 0.400000 0.000000 0.400000 ( 0.402446) | |
# {}[str.to_sym] 0.550000 0.000000 0.550000 ( 0.547712) | |
# {}[long] 0.430000 0.000000 0.430000 ( 0.429800) | |
# {}[long.to_sym] 0.590000 0.010000 0.600000 ( 0.590671) | |
# str[0] 0.250000 0.000000 0.250000 ( 0.253782) | |
# str[-1] 0.250000 0.000000 0.250000 ( 0.250922) | |
# str =~ /(match)/ 1.270000 0.000000 1.270000 ( 1.263257) | |
# str =~ /(miss)/ 0.830000 0.000000 0.830000 ( 0.830235) | |
# unpack * 1.090000 0.000000 1.090000 ( 1.099400) | |
# unpack H40 1.130000 0.000000 1.130000 ( 1.129725) | |
# unpack H40*10 0.600000 0.000000 0.600000 ( 0.596150) | |
# | |
Benchmark.bm(20) do |x| | |
n = 1000000 | |
a = "a" | |
z = ('a'..'z').to_a.join | |
x.report "a" do | |
n.times { a } | |
end | |
x.report "a to_s" do | |
n.times { a.to_s } | |
end | |
x.report "a to_sym" do | |
n.times { a.to_sym } | |
end | |
x.report "a..z to_sym" do | |
n.times { z.to_sym } | |
end | |
hash = {a => 1, z => 2} | |
x.report "{}[str]" do | |
n.times { hash[z] } | |
end | |
hash = {a.to_sym => 1, z.to_sym => 2} | |
x.report "{}[str.to_sym]" do | |
n.times { hash[z.to_sym] } | |
end | |
long = "a" * 40 | |
hash = {} | |
1.upto(40).each do |i| | |
hash[("a" * i)] = i | |
end | |
x.report "{}[long]" do | |
n.times { hash[long] } | |
end | |
long = "a" * 40 | |
hash = {} | |
1.upto(40).each do |i| | |
hash[("a" * i).to_sym] = i | |
end | |
x.report "{}[long.to_sym]" do | |
n.times { hash[long.to_sym] } | |
end | |
x.report "str[0]" do | |
n.times { z[0] } | |
end | |
x.report "str[-1]" do | |
n.times { z[-1] } | |
end | |
regexp = /\Aa.*\z/ | |
x.report "str =~ /(match)/" do | |
n.times { z =~ regexp } | |
end | |
regexp = /\Az\z/ | |
x.report "str =~ /(miss)/" do | |
n.times { z =~ regexp } | |
end | |
x.report("unpack *") do | |
sha = "ee8b5c4ddb843673ef659cfebd0c9e4cbe8ca59d" | |
str = [sha].pack("H*") | |
n.times { str.unpack("H*") } | |
end | |
x.report("unpack H40") do | |
sha = "ee8b5c4ddb843673ef659cfebd0c9e4cbe8ca59d" | |
str = [sha].pack("H40") | |
n.times { str.unpack("H40") } | |
end | |
x.report("unpack H40*10") do | |
sha = "ee8b5c4ddb843673ef659cfebd0c9e4cbe8ca59d" | |
a = Array.new(10, sha) | |
str = a.pack("H40" * 10) | |
(n/10).times { str.unpack("H40" * 10) } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
# Benchmarks for various symbol operations. | |
# | |
# user system total real | |
# :a 0.130000 0.000000 0.130000 ( 0.133211) | |
# :a to_sym 0.210000 0.000000 0.210000 ( 0.212942) | |
# :a to_s 0.540000 0.000000 0.540000 ( 0.536892) | |
# :a..z to_s 0.530000 0.000000 0.530000 ( 0.540183) | |
# {}[:sym] 0.320000 0.000000 0.320000 ( 0.315051) | |
# | |
Benchmark.bm(20) do |x| | |
n = 1000000 | |
a = "a" | |
z = ('a'..'z').to_a.join | |
a = a.to_sym | |
x.report ":a" do | |
n.times { a } | |
end | |
x.report ":a to_sym" do | |
n.times { a.to_sym } | |
end | |
x.report ":a to_s" do | |
n.times { a.to_s } | |
end | |
z = z.to_sym | |
x.report ":a..z to_s" do | |
n.times { z.to_s } | |
end | |
hash = {a => 1, z => 2} | |
x.report "{}[:sym]" do | |
n.times { hash[z] } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
require 'thread' | |
# Benchmarks for various thread operations. | |
# | |
# user system total real | |
# Thread.current 0.030000 0.000000 0.030000 ( 0.026511) | |
# current[]= 0.030000 0.000000 0.030000 ( 0.030801) | |
# current[] 0.030000 0.000000 0.030000 ( 0.027566) | |
# | |
Benchmark.bm(20) do |x| | |
n = 100000 | |
x.report "Thread.current" do | |
n.times { Thread.current } | |
end | |
current = Thread.current | |
x.report "current[]=" do | |
n.times { current[:key] = :value } | |
end | |
x.report "current[]" do | |
n.times { current[:key] } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment