Skip to content

Instantly share code, notes, and snippets.

@TeWu
Last active March 12, 2020 11:49
Show Gist options
  • Save TeWu/c5e47eda22c6c7cd6ee63d818f941d99 to your computer and use it in GitHub Desktop.
Save TeWu/c5e47eda22c6c7cd6ee63d818f941d99 to your computer and use it in GitHub Desktop.

Revisions

  1. TeWu revised this gist Mar 12, 2020. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions ruby_bench_max.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    require 'benchmark/ips'


    def fast(a, b)
    a > b ? a : b
    end
    @@ -8,6 +9,7 @@ def slow(a, b)
    [a, b].max
    end


    class GCSuite
    def warming(*)
    run_gc
    @@ -29,10 +31,9 @@ def run_gc
    end
    end

    suite = GCSuite.new

    Benchmark.ips do |x|
    x.config(:time => 10, :warmup => 2, :suite => suite)
    x.config(time: 10, warmup: 2, suite: GCSuite.new)
    x.stats = :bootstrap
    x.confidence = 95

    @@ -51,4 +52,4 @@ def run_gc
    end
    end
    x.compare!
    end
    end
  2. TeWu created this gist Mar 12, 2020.
    54 changes: 54 additions & 0 deletions ruby_bench_max.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    require 'benchmark/ips'

    def fast(a, b)
    a > b ? a : b
    end

    def slow(a, b)
    [a, b].max
    end

    class GCSuite
    def warming(*)
    run_gc
    end

    def running(*)
    run_gc
    end

    def warmup_stats(*); end
    def add_report(*); end

    private

    def run_gc
    GC.enable
    GC.start
    GC.disable
    end
    end

    suite = GCSuite.new

    Benchmark.ips do |x|
    x.config(:time => 10, :warmup => 2, :suite => suite)
    x.stats = :bootstrap
    x.confidence = 95

    x.report('fast code') do |times|
    i = 0
    while i < times
    fast(100, 200)
    i += 1
    end
    end
    x.report('slow code') do |times|
    i = 0
    while i < times
    slow(100, 200)
    i += 1
    end
    end
    x.compare!
    end