32.2. Benchmark
The Benchmark module can help you determine which of several possible choices executes the fastest. The timethese function runs the specified code segments the number of times requested and reports back how long each segment took. You can get a nicely sorted comparison chart if you call cmpthese the same way. Code segments may be given as function references instead of strings (in fact, they must be if you use lexical variables from the calling scope), but call overhead can influence the timings. If you don't ask for enough iterations to get a good timing, the function emits a warning. Lower-level interfaces are available that run just one piece of code either for some number of iterations (timeit) or for some number of seconds (countit). These functions return Benchmark objects (see the online documentation for a description). With countit, you know it will run in enough time to avoid warnings, because you specified a minimum run time. To get the most out of the Benchmark module, you'll need a good bit of practice. It isn't usually enough to run a couple different algorithms on the same data set, because the timings only reflect how well those algorithms did on that particular data set. To get a better feel for the general case, you'll need to run several sets of benchmarks, varying the data sets used. For example, suppose you wanted to know the best way to get a copy of a string without the last two characters. You think of four ways to do so (there are, of course, several others): chop twice, copy and substitute, or use substr on either the left- or righthand side of an assignment. You test these algorithms on strings of length 2, 200, and 20_000:
which produces the following output:
With small data sets, the "rsubstr" algorithm runs 14% faster than
the "chop2" algorithm, but in large data sets, it runs 45% slower.
On empty data sets (not shown here), the substitution mechanism is the
fastest. So there is often no best solution for all possible cases,
and even these timings don't tell the whole story, since you're still at
the mercy of your operating system and the C library Perl was built
with. What's good for you may be bad for someone else. It takes a
while to develop decent benchmarking skills. In the meantime, it helps
to be a good liar.
![]() Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|