Skip to content

Instantly share code, notes, and snippets.

@danini-the-panini
Last active March 11, 2022 07:35
Show Gist options
  • Save danini-the-panini/883baaa92c4dc4f8ed1a49134600576c to your computer and use it in GitHub Desktop.
Save danini-the-panini/883baaa92c4dc4f8ed1a49134600576c to your computer and use it in GitHub Desktop.
reject! vs next
require 'benchmark/ips'
N = 1_000_000
ARRAY = N.times.map { rand }
Benchmark.ips do |x|
x.report("reject") do
out = []
ARRAY.reject! { _1 >= 0.5 }
ARRAY.each { out << _1 }
end
x.report("next") do
out = []
ARRAY.each { next if _1 >= 0.5; out << _1 }
end
end
Warming up --------------------------------------
reject 3.000 i/100ms
next 5.000 i/100ms
Calculating -------------------------------------
reject 36.357 (± 0.0%) i/s - 183.000 in 5.034273s
next 58.647 (± 1.7%) i/s - 295.000 in 5.030860s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment