Save 37% off PRO during our Black Friday Sale! »

Introduction benchmark-plot at PRUG

Introduction benchmark-plot at PRUG

Here I present an introduction to benchmark-plot, a gem that I've developed for easily creating plots of benchmarks. Presented at Pune Ruby Users Group at the July Meetup.

5083e35c5075b75473919524286239b3?s=128

Sameer Deshmukh

July 02, 2016
Tweet

Transcript

  1. Sameer Deshmukh github.com/v0dro @v0dro

  2. Ruby Science Foundation www.sciruby.com @sciruby @sciruby

  3. None
  4. benchmark­plot

  5. There are three kinds of lies: Lies, damned lies, and

    benchmarks.
  6. Compare the same code before and after refactor. Compare performance

    of two distinct ways of doing the same thing. Curiosity.
  7. Array#map -> Array#flatten a.map { |e| [e,e] }.flatten # =>

    [1,1,2,3,3.... Array#flat_map a.flat_map { |e| [e,e] } # => [1,1,2,2,3,3.... a = (1..size).to_a
  8. require 'benchmark' Benchmark.bm do |x| x.report("map.flat") do (1..1000).to_a.map { |e|

    [e,e] }.flatten end x.report("flat_map") do (1..1000).to_a.flat_map { |e| [e,e] } end end
  9. user system total real map.flat 0.000 0.000 0.000 ( 0.001025)

    flat_map 0.000 0.000 0.000 ( 0.000569)
  10. require 'benchmark' Benchmark.bm do |x| [100, 500, 1000, 5000].each do

    |size| x.report("map.flat") do (1..size).to_a.map { |e| [e,e] }.flatten end x.report("flat_map") do (1..size).to_a.flat_map { |e| [e,e] } end end end
  11. user system total real map.flat 0.000 0.000 0.000 ( 0.000069)

    flat_map 0.000 0.000 0.000 ( 0.000062) map.flat 0.000 0.000 0.000 ( 0.000314) flat_map 0.000 0.000 0.000 ( 0.000259) map.flat 0.000 0.000 0.000 ( 0.000750) flat_map 0.000 0.000 0.000 ( 0.000409) map.flat 0.000 0.000 0.000 ( 0.003270) flat_map 0.010 0.000 0.010 ( 0.001771)
  12. benchmark­plot to the rescue!

  13. Benchmark.plot()

  14. require 'benchmark/plot' arr = [100, 500, 1000, 5000] Benchmark.plot(arr) do

    |x| x.report("map.flat") do |size| (1..size).to_a.map { |e| [e,e] }.flatten end x.report("flat_map") do |size| (1..size).to_a.flat_map { |e| [e,e] } end end
  15. None
  16. require 'benchmark/plot' arr = [100, 500, 1000, 5000] arr.map! {

    |size| (1..size).to_a } Benchmark.plot(arr) do |x| x.report("map.flat") do |a| a.map { |e| [e,e] }.flatten end x.report("flat_map") do |a| a.flat_map { |e| [e,e] } end end
  17. None
  18. require 'benchmark/plot' arr = [100, 500, 1000, 5000] arr.map! {

    |size| (1..size).to_a } Benchmark.plot(arr) do |x| x.report("map.flat") do |a| a.map { |e| [e,e] }.flatten end x.report("flat_map") do |a| a.flat_map { |e| [e,e] } end end
  19. class TestArray attr_reader :data def initialize data @data = data

    end def to_s @data.size.to_s end end
  20. require 'benchmark/plot' arr = [100, 500, 1000, 5000] arr.map! {

    |size| TestArray.new (1..size).to_a } Benchmark.plot(arr) do |x| x.report("map.flat") do |a| a.data.map { |e| [e,e] }.flatten end x.report("flat_map") do |a| a.data.flat_map { |e| [e,e] } end end
  21. None
  22. Customization Options • :title – Change the title of the

    graph. • :x_labels – Toggle whether X axis labels should be displayed. • :x_axis_label – A string for the label of the X axis. • :time – The kind of time that you want plotted (real time or system CPU time or user CPU time). • :file_name – The file name of the graph.
  23. Thank You PRUG!

  24. Any questions?