Simple Profiling in Python


Scripting languages are relatively slow compared to native languages e.g. C/C++. Therefore, it is often needed to identify the performance bottlenecks of the code. In [here], the timeit function is useful in compare the timing of different pieces of code. The profile package of Python provides a quck solution to do a simple profiler for a piece of code.

#!/usr/bin/env python
# https://helloacm.com

import profile

def test1():
    s = 0
    for i in xrange(1, 10**8):
        s += i s

def test2():
    s = 0
    for i in range(1, 10**8):
        s += i
    return s

if __name__ == "__main__":
    profile.run("test1()");
    profile.run("test2()");

Similar to pickle package having an enhanced C-version cPickle, profile can be safely replaced by cProfile. The above code gives the following comparison.

3 function calls in 10.757 seconds

Ordered by: standard name

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
 1    0.000    0.000   10.757   10.757 string:1(module)
 1   10.757   10.757   10.757   10.757 test.py:6(test1)
 1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

4 function calls in 12.698 seconds

Ordered by: standard name

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
 1    0.000    0.000   12.698   12.698 string:1(module)
 1   11.205   11.205   12.698   12.698 test.py:12(test2)
 1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
 1    1.493    1.493    1.493    1.493 {range}

From the timing result, it is clear that using xrange is better than range because it uses less memory and faster. The range will actually generate the whole list of elements which takes time and consumes memory if the list is large.

The ncalls is the number of calls. The tottime gives the total time spent in the function, this excludes the time made in calls to sub-functions. percall is the quotient of tottime divided by ncalls. The cumtime counts the total time spent in this and all subfunctions. This is from invocation to exit and it is accurate even for recursive functions. The percall is the quotient of cumtime divided by primitive calls. The last column filename:lineno(function) provides the respective data of each function.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
a WordPress rating system
409 words
Last Post: Codeforces: B. Little Elephant and Numbers
Next Post: Self-executing Anonymous Functions in Javascript

The Permanent URL is: Simple Profiling in Python

Leave a Reply