Skip to content

Instantly share code, notes, and snippets.

@maxim
Last active May 13, 2023 12:17
Show Gist options
  • Select an option

  • Save maxim/58105aa34f18b64cd2583fe1c857c060 to your computer and use it in GitHub Desktop.

Select an option

Save maxim/58105aa34f18b64cd2583fe1c857c060 to your computer and use it in GitHub Desktop.

Revisions

  1. maxim revised this gist May 13, 2023. 1 changed file with 15 additions and 1 deletion.
    16 changes: 15 additions & 1 deletion bench.rb
    Original file line number Diff line number Diff line change
    @@ -16,4 +16,18 @@
    }

    x.compare!
    end
    end

    # Warming up --------------------------------------
    # catch a throw 347.786k i/100ms
    # rescue and exception 135.262k i/100ms
    # return string 795.463k i/100ms
    # Calculating -------------------------------------
    # catch a throw 3.465M (± 0.4%) i/s - 17.389M in 5.019335s
    # rescue and exception 1.359M (± 2.0%) i/s - 6.898M in 5.077732s
    # return string 7.949M (± 0.4%) i/s - 39.773M in 5.003773s

    # Comparison:
    # return string: 7948778.8 i/s
    # catch a throw: 3464519.8 i/s - 2.29x slower
    # rescue and exception: 1359120.9 i/s - 5.85x slower
  2. maxim revised this gist May 13, 2023. 1 changed file with 7 additions and 22 deletions.
    29 changes: 7 additions & 22 deletions bench.rb
    Original file line number Diff line number Diff line change
    @@ -1,34 +1,19 @@
    # THIS BENCH TURNED OUT INVALID. SEE GIST COMMENT.
    code1 = -> { catch(:foo) { throw(:foo, 'string') } }
    code2 = -> { begin; raise RuntimeError; rescue RuntimeError; 'string' end }
    code3 = -> { 'string' }

    Benchmark.ips do |x|
    code = -> { catch(:foo) { throw(:foo, 'string') } }
    x.report("catch a throw") {
    value = code.call
    value = code1.call
    }

    code = -> { begin; raise RuntimeError; rescue RuntimeError; 'string' end }
    x.report("rescue and exception") {
    value = code.call
    value = code2.call
    }

    code = -> { 'string' }
    x.report("return string") {
    value = code.call
    value = code3.call
    }

    x.compare!
    end

    # Warming up --------------------------------------
    # catch a throw 800.000k i/100ms
    # rescue and exception 806.001k i/100ms
    # return string 808.500k i/100ms
    # Calculating -------------------------------------
    # catch a throw 8.056M (± 0.4%) i/s - 40.800M in 5.064886s
    # rescue and exception 8.037M (± 0.4%) i/s - 40.300M in 5.014526s
    # return string 8.017M (± 2.4%) i/s - 40.425M in 5.045557s

    # Comparison:
    # catch a throw: 8055618.1 i/s
    # rescue and exception: 8036783.6 i/s - same-ish: difference falls within error
    # return string: 8017444.4 i/s - same-ish: difference falls within error
    end
  3. maxim revised this gist May 13, 2023. No changes.
  4. maxim revised this gist May 13, 2023. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions bench.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # THIS BENCH TURNED OUT INVALID. SEE GIST COMMENT.

    Benchmark.ips do |x|
    code = -> { catch(:foo) { throw(:foo, 'string') } }
    x.report("catch a throw") {
  5. maxim revised this gist May 10, 2023. No changes.
  6. maxim revised this gist May 10, 2023. 1 changed file with 10 additions and 10 deletions.
    20 changes: 10 additions & 10 deletions bench.rb
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@
    value = code.call
    }

    code = -> { "string" }
    code = -> { 'string' }
    x.report("return string") {
    value = code.call
    }
    @@ -18,15 +18,15 @@
    end

    # Warming up --------------------------------------
    # catch a throw 809.111k i/100ms
    # rescue and exception 809.739k i/100ms
    # return string 807.068k i/100ms
    # catch a throw 800.000k i/100ms
    # rescue and exception 806.001k i/100ms
    # return string 808.500k i/100ms
    # Calculating -------------------------------------
    # catch a throw 8.048M (± 0.8%) i/s - 40.456M in 5.027045s
    # rescue and exception 8.050M (± 0.8%) i/s - 40.487M in 5.029488s
    # return string 8.040M0.4%) i/s - 40.353M in 5.018972s
    # catch a throw 8.056M (± 0.4%) i/s - 40.800M in 5.064886s
    # rescue and exception 8.037M (± 0.4%) i/s - 40.300M in 5.014526s
    # return string 8.017M2.4%) i/s - 40.425M in 5.045557s

    # Comparison:
    # rescue and exception: 8050433.0 i/s
    # catch a throw: 8048082.4 i/s - same-ish: difference falls within error
    # return string: 8040326.6 i/s - same-ish: difference falls within error
    # catch a throw: 8055618.1 i/s
    # rescue and exception: 8036783.6 i/s - same-ish: difference falls within error
    # return string: 8017444.4 i/s - same-ish: difference falls within error
  7. maxim created this gist May 10, 2023.
    32 changes: 32 additions & 0 deletions bench.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    Benchmark.ips do |x|
    code = -> { catch(:foo) { throw(:foo, 'string') } }
    x.report("catch a throw") {
    value = code.call
    }

    code = -> { begin; raise RuntimeError; rescue RuntimeError; 'string' end }
    x.report("rescue and exception") {
    value = code.call
    }

    code = -> { "string" }
    x.report("return string") {
    value = code.call
    }

    x.compare!
    end

    # Warming up --------------------------------------
    # catch a throw 809.111k i/100ms
    # rescue and exception 809.739k i/100ms
    # return string 807.068k i/100ms
    # Calculating -------------------------------------
    # catch a throw 8.048M (± 0.8%) i/s - 40.456M in 5.027045s
    # rescue and exception 8.050M (± 0.8%) i/s - 40.487M in 5.029488s
    # return string 8.040M (± 0.4%) i/s - 40.353M in 5.018972s

    # Comparison:
    # rescue and exception: 8050433.0 i/s
    # catch a throw: 8048082.4 i/s - same-ish: difference falls within error
    # return string: 8040326.6 i/s - same-ish: difference falls within error