Skip to content

Instantly share code, notes, and snippets.

@schneems
Created October 12, 2018 01:08
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 schneems/78164e61485bf3d130e35e7347d61ae0 to your computer and use it in GitHub Desktop.
Save schneems/78164e61485bf3d130e35e7347d61ae0 to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
MYARRAY = [["a", ["b", nil, "c", ["d"]]]]
Benchmark.ips do |x|
x.report("before") {
array = MYARRAY.dup
array.flatten!
array.uniq!
array.compact!
array
}
x.report("after ") {
array = MYARRAY.dup
array.tap(&:flatten!).tap(&:uniq!).tap(&:compact!)
}
x.compare!
end
# Warming up --------------------------------------
# before 35.455k i/100ms
# after 32.321k i/100ms
# Calculating -------------------------------------
# before 393.043k (± 4.5%) i/s - 1.985M in 5.062618s
# after 371.538k (± 2.2%) i/s - 1.875M in 5.048124s
# Comparison:
# before: 393043.0 i/s
# after : 371537.7 i/s - same-ish: difference falls within error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment