Linux Format

Benchmarki­ng

-

It's easy to take benchmarks too seriously – graphics card enthusiast­s have been doing so for years. In our tutorial we use the cProfile module which enables you to count and time each function call. This can provide valuable data about where the bottleneck­s in your code are, which might not be immediatel­y obvious.

CProfile is designed to be as lightweigh­t and unobtrusiv­e as possible, but if you have a tiny function that’s called millions of times, then that's millions of tallying calls and they all add up. If the function is really small, this means that more time is spent benchmarki­ng than is spent doing whatever it is the function does, and so the result is largely meaningles­s. If you're confident a small function can't be sped up any more, it's best to disable the profiler. If you're not so confident, then by all means continue to benchmark such small functions, but rest assured that they will work a lot faster without the profiler interferin­g.

If you just want to measure 'wall time', which is the total time it takes for your code fragment to run, then the module may be more appropriat­e. For example to test (from the interprete­r) a function called from a module three times:

The default for is a million, which is why it’s a good idea to specify your own value here. You will need to specify your module in the parameter even if you have previously imported it, since will not inherit this namespace.

Newspapers in English

Newspapers from Australia