Skip to content

Instantly share code, notes, and snippets.

@asterite
Created January 10, 2014 14:32
Show Gist options
  • Select an option

  • Save asterite/8354560 to your computer and use it in GitHub Desktop.

Select an option

Save asterite/8354560 to your computer and use it in GitHub Desktop.
Benchmark time lost in Ruby because of calls
$x = 0
def foo1
foo2
end
def foo2
foo3
end
def foo3
foo4
end
def foo4
foo5
end
def foo5
foo6
end
def foo6
foo7
end
def foo7
$x += 1
end
$x = 0
time = Time.now
10_000_000.times { foo1 }
puts "foo1: #{Time.now - time}"
$x = 0
time = Time.now
10_000_000.times { foo2 }
puts "foo2: #{Time.now - time}"
$x = 0
time = Time.now
10_000_000.times { foo3 }
puts "foo3: #{Time.now - time}"
$x = 0
time = Time.now
10_000_000.times { foo4 }
puts "foo4: #{Time.now - time}"
$x = 0
time = Time.now
10_000_000.times { foo5 }
puts "foo5: #{Time.now - time}"
time = Time.now
10_000_000.times { foo6 }
puts "foo6: #{Time.now - time}"
$x = 0
time = Time.now
10_000_000.times { foo7 }
puts "foo7: #{Time.now - time}"
$x = 0
time = Time.now
10_000_000.times do
$x += 1
end
puts "direct: #{Time.now - time}"
# Ruby
# ----
# foo1: 2.652264
# foo2: 2.393971
# foo3: 2.113695
# foo4: 1.864027
# foo5: 1.599237
# foo6: 1.313431
# foo7: 0.837017
# direct: 0.561372
# Crystal
# -------
# foo1: 0.0893159
# foo2: 0.0781229
# foo3: 0.0636261
# foo4: 0.0531781
# foo5: 0.0419328
# foo6: 0.0332189
# foo7: 0.033031
# direct: 0.036957
# Crystal (compiled with --release)
# ---------------------------------
# foo1: 1.00136e-05
# foo2: 0
# foo3: 0
# foo4: 1.19209e-06
# foo5: 0
# foo6: 0
# foo7: 0
# direct: 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment