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.

Revisions

  1. danini-the-panini revised this gist Mar 11, 2022. 2 changed files with 15 additions and 7 deletions.
    10 changes: 7 additions & 3 deletions main.rb
    Original file line number Diff line number Diff line change
    @@ -5,13 +5,17 @@

    Benchmark.ips do |x|
    x.report("reject") do
    arr = ARRAY.dup
    out = []
    ARRAY.reject! { _1 >= 0.5 }
    ARRAY.each { out << _1 }
    arr.reject! { _1 >= 0.5 }
    arr.each { out << _1 }
    end

    x.report("next") do
    arr = ARRAY.dup
    out = []
    ARRAY.each { next if _1 >= 0.5; out << _1 }
    arr.each { next if _1 >= 0.5; out << _1 }
    end
    x.hold! 'temp_results'
    x.compare!
    end
    12 changes: 8 additions & 4 deletions result.txt
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,10 @@
    Warming up --------------------------------------
    reject 3.000 i/100ms
    next 5.000 i/100ms
    reject 2.000 i/100ms
    next 2.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
    reject 20.503 (± 0.0%) i/s - 104.000 in 5.072750s
    next 25.475 (± 0.0%) i/s - 128.000 in 5.025467s

    Comparison:
    next: 25.5 i/s
    reject: 20.5 i/s - 1.24x (± 0.00) slower
  2. danini-the-panini revised this gist Mar 11, 2022. 2 changed files with 0 additions and 6 deletions.
    File renamed without changes.
    6 changes: 0 additions & 6 deletions ruby 2.7.5
    Original file line number Diff line number Diff line change
    @@ -1,6 +0,0 @@
    Warming up --------------------------------------
    reject 2.000 i/100ms
    next 3.000 i/100ms
    Calculating -------------------------------------
    reject 25.188 (± 0.0%) i/s - 126.000 in 5.002970s
    next 34.862 (± 0.0%) i/s - 177.000 in 5.077603s
  3. danini-the-panini created this gist Mar 11, 2022.
    17 changes: 17 additions & 0 deletions main.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    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
    6 changes: 6 additions & 0 deletions ruby 2.7.5
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    Warming up --------------------------------------
    reject 2.000 i/100ms
    next 3.000 i/100ms
    Calculating -------------------------------------
    reject 25.188 (± 0.0%) i/s - 126.000 in 5.002970s
    next 34.862 (± 0.0%) i/s - 177.000 in 5.077603s
    6 changes: 6 additions & 0 deletions ruby 3.1.1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    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