Saturday, December 30, 2006

[Pages 162-3] But it's too slow ! - Benchmark


Listen to this article.

Value: High

Level: Easy

Summary:
Typically, slow-running programs have one or two performance graveyards, places where execution time goes to die. The trick is finding them. The Benchmark module and the Ruby profilers can help.
 
You can use the Benchmark module to time sections of code. For example, we may wonder which is faster: a large loop using variables local to the loop's block or using variables from the surrounding scope.

You have to be careful when benchmarking, because oftentimes Ruby programs can run slowly because of the overhead of garbage collection. Because this garbage collection can happen any time during your program's execution, you may find that benchmarking gives misleading results, showing a section of code running slowly when in fact the slowdown was caused because garbage collection happened to trigger while that code was executing. The Benchmark module has the bmbm method that runs the tests twice, once as a rehearsal and once to measure performance, in an attempt to minimize the distortion introduced by garbage collection. The benchmarking process itself is relatively well mannered—it doesn't slow down your program much.

Memo: Garbage collection could give you misleading results when benchmarking.

Example:
require 'benchmark'
include Benchmark
LOOP_COUNT = 1_000_000
bm(12) do |test|
test.report("normal:") do
LOOP_COUNT.times do |x|
y = x + 1
end
end
test.report("predefine:") do
x = y = 0
LOOP_COUNT.times do |x|
y = x + 1
end
end
end

Reported errata (at 10/17/06 14:17:18 PDT): 0

Errata I found: 1
*On page 162 "bmbm" should be "bm".

My suggestions to the author: 0

Doubts: 2
*What is garbage collection ?
*Why is a large loop using variables local to the loop's block slower than a loop using variables from the surrounding scope ?

0 Comments:

Post a Comment

<< Home