Skip to content

Instantly share code, notes, and snippets.

@chiting
Created August 23, 2015 23:32
Show Gist options
  • Save chiting/72fbc06302dab308890f to your computer and use it in GitHub Desktop.
Save chiting/72fbc06302dab308890f to your computer and use it in GitHub Desktop.
require "benchmark/ips"
C = "123456789012345678901"
A = "1234567890123456789012"
B = "12345678901234567890123"
D = "123456789012345678901234"
def slow
B + "x"
end
def fast
A + "x"
end
def slow_ref
D + "x"
end
def fast_ref
C + "x"
end
Benchmark.ips do |x|
x.report("#{C.length} chars") { fast_ref }
x.report("#{A.length} chars") { fast }
x.report("#{B.length} chars") { slow }
x.report("#{D.length} chars") { slow_ref }
end
@chiting
Copy link
Author

chiting commented Apr 23, 2019

With ruby 2.6.3 on mac OS 10.14.4 (which makes sense as the internals for String didn't change much since 1.9.3)

Warming up --------------------------------------
            21 chars   273.676k i/100ms
            22 chars   240.588k i/100ms
            23 chars   175.628k i/100ms
            24 chars   174.513k i/100ms
Calculating -------------------------------------
            21 chars      8.209M (± 7.5%) i/s -     40.778M in   5.015295s
            22 chars      8.366M (± 8.0%) i/s -     41.622M in   5.023808s
            23 chars      3.143M (± 7.3%) i/s -     15.631M in   5.013088s
            24 chars      3.112M (± 6.3%) i/s -     15.532M in   5.017577s

Comparison:
            22 chars:  8365985.5 i/s
            21 chars:  8208626.6 i/s - same-ish: difference falls within error
            23 chars:  3143202.6 i/s - 2.66x  slower
            24 chars:  3112300.3 i/s - 2.69x  slower

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment