Is there a way to check the performance of a command in the console in Ruby on Rails?

benchmarking, ruby, ruby-on-rails

Solution

There is! And you don't even need Rails. Look into benchmark from the standard library.

As a sample:

require 'benchmark'

puts Benchmark.measure { [val2,val3,val4,val5,val6].find{|x| x != val1} }
puts Benchmark.measure { [val2,val3,val4,val5,val6].all?{|x| x == val1} }

The report that is output will show (in seconds):

- User CPU time.

- System CPU time.

- Sum of the User and System CPU times.

- The elapsed real time.

Something that looks like this:

0.350000   0.010000   0.360000 (  0.436450)

Problem

I couldn't find any information on if this was possible but it would be useful I could call a method on a command in the `rails console` and determine the performance using any measurement but I was mostly thinking about in time. For example, I'm trying to figure out which of these is faster: ``` [val2,val3,val4,val5,val6].find{|x| x != val1} [val2,val3,val4,val5,val6].all?{|x| x == val1} ``` Is there something like this? ``` [val2,val3,val4,val5,val6].find{|x| x != val1}.performance ```

Original source