Skip to content

Instantly share code, notes, and snippets.

@rmm5t
Last active June 18, 2019 20:36
Show Gist options
  • Select an option

  • Save rmm5t/5e762b0d6cb6edaeb481ea8e26b58b95 to your computer and use it in GitHub Desktop.

Select an option

Save rmm5t/5e762b0d6cb6edaeb481ea8e26b58b95 to your computer and use it in GitHub Desktop.

Revisions

  1. rmm5t revised this gist May 28, 2019. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions benchmark_style_multiple_comparison.rb
    Original file line number Diff line number Diff line change
    @@ -18,10 +18,10 @@ def test_include(n)
    x.report("Array#include? last") { n.times { test_include(7) } }
    end

    # >> user system total real
    # >> conditionals first 0.004635 0.000002 0.004637 ( 0.004636)
    # >> Arra#include? first 0.008265 0.000004 0.008269 ( 0.008277)
    # >> conditionals mid 0.007035 0.000001 0.007036 ( 0.007036)
    # >> #include? mid 0.009721 0.000002 0.009723 ( 0.009723)
    # >> conditionals last 0.008882 0.000001 0.008883 ( 0.008884)
    # >> #include? last 0.011406 0.000003 0.011409 ( 0.011408)
    # >> user system total real
    # >> conditionals first 0.004439 0.000002 0.004441 ( 0.004440)
    # >> Array#include? first 0.008101 0.000002 0.008103 ( 0.008103)
    # >> conditionals mid 0.006777 0.000000 0.006777 ( 0.006777)
    # >> Array#include? mid 0.009760 0.000002 0.009762 ( 0.009761)
    # >> conditionals last 0.009099 0.000001 0.009100 ( 0.009100)
    # >> Array#include? last 0.011387 0.000002 0.011389 ( 0.011390)
  2. rmm5t revised this gist May 28, 2019. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions benchmark_style_multiple_comparison.rb
    Original file line number Diff line number Diff line change
    @@ -10,12 +10,12 @@ def test_include(n)

    n = 100_000
    Benchmark.bmbm(15) do |x|
    x.report("conditionals first") { n.times { test_conditionals(1) } }
    x.report("Arra#include? first") { n.times { test_include(1) } }
    x.report("conditionals mid") { n.times { test_conditionals(4) } }
    x.report("#include? mid") { n.times { test_include(4) } }
    x.report("conditionals last") { n.times { test_conditionals(7) } }
    x.report("#include? last") { n.times { test_include(7) } }
    x.report("conditionals first") { n.times { test_conditionals(1) } }
    x.report("Array#include? first") { n.times { test_include(1) } }
    x.report("conditionals mid") { n.times { test_conditionals(4) } }
    x.report("Array#include? mid") { n.times { test_include(4) } }
    x.report("conditionals last") { n.times { test_conditionals(7) } }
    x.report("Array#include? last") { n.times { test_include(7) } }
    end

    # >> user system total real
  3. rmm5t renamed this gist May 28, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. rmm5t created this gist May 27, 2019.
    27 changes: 27 additions & 0 deletions benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    require 'benchmark'

    def test_conditionals(n)
    n == 1 || n == 2 || n == 3 || n == 4 || n == 5 || n == 6 || n == 7
    end

    def test_include(n)
    [1, 2, 3, 4, 5, 6, 7].include?(n)
    end

    n = 100_000
    Benchmark.bmbm(15) do |x|
    x.report("conditionals first") { n.times { test_conditionals(1) } }
    x.report("Arra#include? first") { n.times { test_include(1) } }
    x.report("conditionals mid") { n.times { test_conditionals(4) } }
    x.report("#include? mid") { n.times { test_include(4) } }
    x.report("conditionals last") { n.times { test_conditionals(7) } }
    x.report("#include? last") { n.times { test_include(7) } }
    end

    # >> user system total real
    # >> conditionals first 0.004635 0.000002 0.004637 ( 0.004636)
    # >> Arra#include? first 0.008265 0.000004 0.008269 ( 0.008277)
    # >> conditionals mid 0.007035 0.000001 0.007036 ( 0.007036)
    # >> #include? mid 0.009721 0.000002 0.009723 ( 0.009723)
    # >> conditionals last 0.008882 0.000001 0.008883 ( 0.008884)
    # >> #include? last 0.011406 0.000003 0.011409 ( 0.011408)