The same task, measured
The task
The task is deliberately boring: add up the squares of the numbers from one to five million. The same loop, the same arithmetic, no library differences to argue about. Both programs print the total and the seconds they took.
Ruby
require "benchmark" time = Benchmark.realtime do sum = 0 1.upto(5_000_000) { |i| sum += i * i } puts sum end puts "#{time.round(3)}s"
ruby bench.rb
41666679166667500000
1.051s
Python
import time start = time.perf_counter() total = 0 for i in range(1, 5_000_001): total += i * i print(total) print(f"{time.perf_counter() - start:.3f}s")
python3 bench.py
41666679166667500000
1.806s
The same total, because both add the same integers. Ruby took about 1.05 seconds, Python about 1.81, on the same machine in the same minute.
What the numbers say
A single loop over integers favours CRuby here: the arithmetic and the block are compiled to bytecode and run without the per-iteration object work that CPython does for every integer. Change the task and the result can change with it: string work, regular expressions, JSON and numeric libraries have their own implementations in each language.
- One run is not a benchmark. Repeat it, change the size, and look at the spread before believing a difference.
- Compare the same code. A Ruby version that uses sum from Enumerable, or a Python version that uses NumPy, measures the library, not the loop.
- Measure the program you ship. Startup time, gem loading and I/O often dominate a script that runs for a second; the loop above takes a second by itself and says little about a real program.
Both languages are fast enough for the scripts this course builds, and both have a profiler for the case where they are not: lesson 12.9 for Ruby, cProfile for Python. The useful habit is the same in both: measure, find the hot function, then change one thing.
What to watch for
Do not move a service between languages because of a micro-benchmark. The
cost of a service is usually the database, the network and the deploy,
and those do not change with the interpreter.
When you do compare, say what you measured: the version, the machine, the
task and the repetition count. A number without those four is an opinion.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Why is one run of a benchmark not a result?
Answer
The spread between runs can be larger than the difference between the two programs
Repeat the run and look at the spread before believing a difference of a few percent.
-
What does this program print?
sum = 0 1.upto(100) { |i| sum += i * i } puts sumAnswer
338350
The loop adds the square of each number from one to a hundred, which is the shape of the five-million task.
-
What does this program print?
require "benchmark" p Benchmark.realtime { 1.upto(10) { |i| i } }.classAnswer
Float
realtime returns the elapsed seconds as a Float, which is why the page rounds it to three places.
-
What does this program print?
p 1.23456.round(3)Answer
1.235
round keeps three decimal places, which is the formatting both versions use for their seconds.
-
What happens to a Ruby integer that grows past the machine word?
Answer
It keeps growing as a Bignum, with no overflow
The total of the task is far past 64 bits, and both languages still print it exactly.
-
What does this program print?
p 2**100 > 0Answer
true
The power is an exact integer of any size, and it is positive.
-
Why compare the same code in both languages?
Answer
A Ruby version with sum or a Python version with NumPy measures the library, not the loop
The comparison is only about the language when the two programs do the same work.
-
What does this program print?
puts (1..10).sumAnswer
55
The range adds its ten numbers, which is the Ruby one-liner for the loop the page writes out.
-
What does being faster in this loop say about a whole service?
Answer
Little: the database, the network and the deploy usually dominate
A micro-benchmark measures one loop, and a service spends its time elsewhere.
-
What four things should a comparison name?
Answer
The version, the machine, the task and the repetition count
A number without those four is an opinion, as the page says.
Article author: Arthur Isaev