Skip to content

Instantly share code, notes, and snippets.

@hamin
Created January 31, 2012 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamin/1707821 to your computer and use it in GitHub Desktop.
Save hamin/1707821 to your computer and use it in GitHub Desktop.
Inject performance
require 'benchmark'
n = 100
Benchmark.bm do |x|
x.report("each") { num=0;[1..n].each{ |i| num+= 1} }
x.report("inject") { [1..n].inject(0){ |s,i| s+= 1} }
end
n = 1000
Benchmark.bm do |x|
x.report("each") { num=0;[1..n].each{ |i| num+= 1} }
x.report("inject") { [1..n].inject(0){ |s,i| s+= 1} }
end
n = 10000000
Benchmark.bm do |x|
x.report("each") { num=0;[1..n].each{ |i| num+= 1} }
x.report("inject") { [1..n].inject(0){ |s,i| s+= 1} }
end
n = 1000000000
Benchmark.bm do |x|
x.report("each") { num=0;[1..n].each{ |i| num+= 1} }
x.report("inject") { [1..n].inject(0){ |s,i| s+= 1} }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment