You can also try cProfile
which is a standard builtin Python profiler that is recommended for most users.
It gives both total running time in addition to the total run time of each function and the number of times each function was called.
Here is an example of how to use it when running your script. The results are saved to a file named 'output_stats':
import cProfile
import pstats
cProfile.run(open('primes.py', 'rb'), 'output_stats')
p = pstats.Stats('output_stats')
p.sort_stats('cumulative').print_stats(10)
Thu May 14 09:26:09 2015 output_stats
369 function calls in 0.213 seconds
Ordered by: cumulative time
List reduced from 89 to 10 due to restriction <10>
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.213 0.213 primes.py:1(<module>)
1 0.019 0.019 0.213 0.213 primes.py:22(prime)
2 0.141 0.070 0.181 0.091 primes.py:1(primes)
3 0.041 0.014 0.041 0.014 {range}
4 0.000 0.000 0.013 0.003 /usr/local/miniconda/envs/dev/lib/python2.7/site-packages/IPython/kernel/zmq/iostream.py:207(write)
1 0.000 0.000 0.010 0.010 /usr/local/miniconda/envs/dev/lib/python2.7/site-packages/IPython/kernel/zmq/iostream.py:151(flush)
1 0.000 0.000 0.010 0.010 /usr/local/miniconda/envs/dev/lib/python2.7/site-packages/IPython/kernel/zmq/session.py:589(send)
1 0.000 0.000 0.009 0.009 /usr/local/miniconda/envs/dev/lib/python2.7/site-packages/IPython/kernel/zmq/session.py:530(serialize)
4 0.000 0.000 0.007 0.002 /usr/local/miniconda/envs/dev/lib/python2.7/site-packages/IPython/kernel/zmq/session.py:84(<lambda>)
4 0.000 0.000 0.007 0.002 /usr/local/miniconda/envs/dev/lib/python2.7/site-packages/zmq/utils/jsonapi.py:31(dumps)
===
Example script file named primes.py:
def primes(n):
if n == 2:
return [2]
elif n < 2:
return []
s=range(3, n + 1, 2)
mroot = n ** 0.5
half=(n + 1) / 2 - 1
i = 0
m = 3
while m <= mroot:
if s[i]:
j = (m * m - 3) / 2
s[j] = 0
while j < half:
s[j] = 0
j += m
i = i + 1
m = 2 * i + 3
return [2] + [x for x in s if x]
def prime(a, b):
print(primes(a))
print(primes(b))
if __name__ == "__main__":
prime(10, 100)