Man With Code 👨‍💻

In which I occasionally teach you things.

Programming With Ruby Episode 16, Benchmarking

Series: Ruby Programming

Covered In This Episode: Transcript: Hello Everybody and welcome to Programming With Ruby Episode 16, Benchmarking. I'm Tyler and this video is brought to you by manwithcode.com. In this episode I will tell you what benchmarking is. You will learn how to preform benchmarking tests on some of your code. And after that you will learn how to preform the more exhaustive benchmarking process called profiling. This should be a very quick and easy episode, so lets get started! What is benchmarking? Basically benchmarking is measuring how fast your code runs. Whether that means your code as a whole or only parts of it. This can be useful so you can optimize your code to run faster, in the places it is running the slowest. Benchmarking is also commonly used to compare two different programs in the category of speed, which can be a selling point for many products. Benchmarking To get access to benchmarking make sure you put: [ruby] require 'benchmark' [/ruby] in your code. The most simple form of benchmarking is Benchmark.measure: [ruby] a = Benchmark.measure do 1_000_000.times do |i| x = i end end puts a #=> 0.400000   0.140000   0.540000 (  0.537934) [/ruby] The last number is the actual time it took to run the test. There is also Benchmark.bm, which is similar, but adds headings and allows you to do multiple tests. [ruby] Benchmark.bm do |bm| bm.report('Test 1:') do 1_000_000.times do x = 1 end end bm.report('Test 2:') do 1_000.times do x = "Moo..." end end end # Example Output: #          user     system      total        real # Test 1:  0.430000   0.120000   0.550000 (  0.563787) # Test 2:  0.000000   0.000000   0.000000 (  0.000775) [/ruby] Then there is Benchmark.bmbm, which is exactly the same as bm, but preforms a benchmark twice. [source] Example Benchmark.bmbm Output: Rehearsal ------------------------------------------- Test 1:   0.370000   0.110000   0.480000 (  0.484865) Test 2:   0.000000   0.000000   0.000000 (  0.000529) ---------------------------------- total: 0.480000sec user     system      total        real Test 1:   0.390000   0.090000   0.480000 (  0.477402) Test 2:   0.000000   0.000000   0.000000 (  0.000529) [/source] And that is all there is to know about simple benchmarking, on to profiling. Profiling Profiling takes benchmarking to the extreme. It tells you how much time each part of your code is take, and all you have to do is put: [ruby] require 'profile' [/ruby] at the top of your program! [ruby] require 'profile' class MyMath # Don't worry about the math, just the profiling output # We repeat the code to make it use up more time def self.x_offset angle, distance 1000.times { distance * Math.sin(angle * Math::PI/180) } end def self.y_offset angle, distance 1000.times { distance * Math.cos(angle * Math::PI/180) * -1 } end end MyMath.x_offset(220, 50) MyMath.y_offset(220, 50) [/ruby] And from the profiling output, we can see what took the longest: [source] %   cumulative   self              self     total time   seconds   seconds    calls  ms/call  ms/call  name 72.41     0.21      0.21        2   105.00   145.00  Integer#times 10.34     0.24      0.03     4000     0.01     0.01  Fixnum#* 6.90     0.26      0.02     1000     0.02     0.02  Math.cos 6.90     0.28      0.02     2000     0.01     0.01  Float#/ 3.45     0.29      0.01     1000     0.01     0.01  Float#* 0.00     0.29      0.00        1     0.00   140.00  MyMath#y_offset 0.00     0.29      0.00        1     0.00   150.00  MyMath#x_offset 0.00     0.29      0.00        1     0.00     0.00  Class#inherited 0.00     0.29      0.00     1000     0.00     0.00  Math.sin 0.00     0.29      0.00        2     0.00     0.00  Kernel.singleton_method_added 0.00     0.29      0.00        1     0.00   290.00  #toplevel [/source] And that is it for today's episode! Please do not forget to show your appreciation by donating. If you have any thing to say about anything related to Man With Code or these videos, please leave a comment below, or email me at [email protected] Thanks for watching, Bye!