Skip to content

Instantly share code, notes, and snippets.

@arthurnn
Created February 13, 2014 14:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthurnn/8976458 to your computer and use it in GitHub Desktop.
Save arthurnn/8976458 to your computer and use it in GitHub Desktop.
require 'benchmark'
arr = (1..100).to_a
Benchmark.bm(20) do |x|
x.report("each.with_index") do
100_000.times do
arr.each_with_index do |v, i|
end
end
end
x.report("each.with_index b&") do
100_000.times do
bv = arr.each.with_index
arr.length.times do |i|
bv.next
end
end
end
end
@arthurnn
Copy link
Author

                           user     system      total        real
each.with_index        0.590000   0.000000   0.590000 (  0.591757)
each.with_index b&    11.630000   0.040000  11.670000 ( 11.685133)

@gnomex
Copy link

gnomex commented Feb 13, 2014

                       user     system      total        real

each.with_index 0.780000 0.000000 0.780000 ( 0.787362)
each.with_index b& 7.700000 1.980000 9.680000 ( 9.699257)

@tylermercier
Copy link

Seems a Fiber adds about 1 ms per call. Wonder what this would look like if you compared Fibers against Threads.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment