Is there a way to test command performance in a console in Ruby on Rails?

I could not find any information on whether this is possible, but it would be useful so that I could call the method on a command in the rails console and determine the performance using any measurements, but I was mostly thinking about time.

For example, I'm trying to figure out which one 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 
+8
benchmarking ruby ruby-on-rails
source share
2 answers

There is! And you don’t even need Rails. Check out the benchmark from the standard library.

As an example:

 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 will be displayed will be displayed (in seconds):

  • User CPU time.
  • CPU system time.
  • The sum of the user and system runtimes.
  • Elapsed real time.

Something like this:

 0.350000 0.010000 0.360000 ( 0.436450) 
+16
source share

This stone: https://github.com/igorkasyanchuk/benchmark_methods

No more code:

 t = Time.now user.calculate_report puts Time.now - t 

Now you can do:

 benchmark :calculate_report # in class 

And just call your method

 user.calculate_report 
0
source share

All Articles