[RUBY] Check performance quickly with irb

The evolution of irb does not stop. The most advanced REPL may be Ruby's irb [^ b]. Multi-line editing, syntax highlighting, auto-indentation, and method name completion were shocking.

[^ b]: I am writing without knowing the REPL of other languages, so I welcome Masakari Oshikari, a lover of each language.

Now, with irb 1.2.8 released on December 20, 2020, a feature called measure was added. This measures the time it takes to evaluate the formula.

Displaying the 1 millionth prime number in Ruby

require "prime"
p Prime.lazy.drop(999999).first

Just write. The answer is 15485863. Suppose you want to know how long this will take place. There are many measuring methods, but if you can do it quickly on irb, that's great.

First of all

gem update irb

Let's install the latest irb with. The latest at the time of writing the article (December 23, 2020) is 1.2.9.

And

irb -r prime

Start irb with. No, of course

irb

Start with

irb(main):001:0> require "prime"

It's fine, but it's a hassle, so it's better to pass the library to use to the -r option.

And

irb(main):001:0> measure

If you type, you will be in the mode [^ mode] that measures the evaluation time of the expression ... but in fact, in versions 1.2.8 and 1.2.9, LoadError will appear due to some mistake [^ le]. ]. Fortunately, a pull request to enable the measure command was issued by @elim and has already been captured.

(Added on 2020-12-25) It was reflected in irb 1.3.0 (released on 2020-12-24). Now you can use measure.

[^ mode]: The term "mode" is not used in the original family. I just call it that in this article.

[^ le]: I'm not sure, but this problem seems to occur when installing as a gem. (Added on 2020-12-25) It seems that there is no problem with irb bundled with Ruby 3.0.0.

At present, it can't be helped

irb(main):001:0> IRB.conf[:MEASURE] = true

Let's hit. I can't remember this, but I don't have to. In the next version you should be able to use measure. (→ Addendum: Now)

This way, every time you evaluate an expression on irb, it will measure the time. Now:

irb(main):002:0> Prime.lazy.drop(999999).first
processing time: 7.395823s
=> 15485863

It was about 7.4 seconds.

To stop the measurement mode

irb(main):003:0> IRB.conf[:MEASURE] = false

And. Once the measure command is available,

irb(main):003:0> measure :off

You should be able to go out.

By the way, there are various things to be careful about in this kind of speed (execution time) measurement.

By default, the measure function of irb seems to only take the time difference before and after processing, so if you measure it when the CPU is busy with other things, a large value will come out. Execution time fluctuates due to various factors. In fact, his 1 millionth prime number was about 6.5 seconds when measured again. (Added on 2020-12-25) Maybe Prime uses memoization, and if you do it again without quitting irb, it will finish in a much shorter time.

Even if the measurement result is 7.395823s, there are not so many significant figures. The only thing that displays a lot of digits is that the Float is displayed as it is. Well, probably the second decimal place (0.01 digit) doesn't make sense.

So

irb(main):004:0> 1+1
processing time: 0.000282s
=> 2

The number "0.000282s" that comes out is completely meaningless. It can only be called "Hotondo Zero Desnay". The execution time of such needs to be obtained by dividing the time of executing many times by the number of repetitions. In that case, the benchmark test library will come into play, and it will not be something to do with irb.

I haven't tried it, but it seems that the measurement and display processing can be customized. In a file called .irbrc in the user's home directory,

IRB.conf[:MEASURE_PROC][:CUSTOM] = proc { |context, code, line_no, &block|
  #And so on
}

It seems that you should describe it with a feeling. This makes it possible, for example, to display the results up to the first decimal place.

See irb's commit message for more details: https://github.com/ruby/irb/commit/3899eaf2e21fcb8b6c1ba3fdb1ac4ec8c9b4798f

Recommended Posts

Check performance quickly with irb
Check compliance with object-oriented exercise
Exciting environment check with mkmf
[Java 8] Duplicate deletion (& duplicate check) with Stream
[Java] Element existence check with Stream
Easy input check with Bean Validation!
Check date correlation with Spring Boot
Check when moss with SimpleDateFormat parse
Encrypt strings with B Crypt quickly
Check your internet connection with Alamofire
Microservices With Docker and Cloud Performance