Testing ruby code with benchmark_suite

2012-01-14 :: ( 2 minutes reading )

Just a couple days ago I found out therubygame.com which challenge us to solve a problem with ruby. The result were measured by the fastest, slowest, shortest, longest, cheaters (yup there are also some rule).

And also I was listening to an episode of ruby rouge on Benchmarking. And there is one tools called benchmark_suite.

So there is this challenge to capitalize first letter of every word. I want to compare my code to the fastest solution there. so i installed the gem, also the ruby-ffi gem that somewhat fix an error while I tried to run the benchmark.

so this is the code to benchmark it

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
  require 'benchmark/ips'

  string = "The small brown & ginger fox JUMPED OVER the gate"

  Benchmark.ips do |x|
    x.report("first") do
      string.gsub(/\w+/){|w| w.capitalize}
    end

    x.report("second") do
      string.split.map(&:capitalize).join ' '
    end
  end

And here are the result

first 55609.3 (±10.7%) i/s - 277202 in 5.048175s (cycle=4471)

second 77996.4 (±10.2%) i/s - 389844 in 5.055319s (cycle=6188)

The first code run 55.609,3 times per second and the latter is 77.996,4 times per second. So the second code run more than the first code in one second. Which mean the second code is faster.

Also the first code run 277.202 times in 5.048175 sec while the second code run 389.844 times in 5.05 sec

So this mean, that regex is more slower then calling capitalized on each element on array

comments powered by Disqus

Archives

English

  • My Desktop With i3, polybar, conky, and rofi
  • Convert Intellij Live Template to vscode Snippet
  • Why for-range behaves differently depending on the size of the element (A peek into go compiler optimization)
  • Testing go 1.5 cross compilation on raspberry pi
  • osx-push-to-talk App
  • Tracking origin of bugs with git bisect
  • Stubbing Time.Now() in golang
  • My account just got hacked by Romanian (Possibly)
  • berks upload core dump
  • Safely sharing credentials with PGP

Indonesia

  • Ruby Fiber apaan sih ?
  • Scale MongoDB dengan Sharding
  • Telepon murah ke Indonesia dengan voip
Labs.Yulrizka.com
twitter github feed
    • Left Panel
    • No Panel
    • Right Panel
  • Home
  • EN
  • ID
  • Today I Learned

© Ahmy Yulrizka 2019. Made with hugo source