I have a code that plays fast _it's order is log(n)_.I have to meter it's play's elapsed time.I used from time.time() and time.clock() but they returned 0.I need a timer witch meter nano second.
How can I do it?

Recommended Answers

All 3 Replies

Simply loop your code a million times and time the whole thing. There is also module timeit.

From command line.

C:\>python -m timeit "for i in xrange(100): pass"
1000000 loops, best of 3: 1.77 usec per loop

#Are tuple faster than list?
C:\>python -m timeit (1,2,3,4)
10000000 loops, best of 3: 0.0226 usec per loop

C:\>python -m timeit [1,2,3,4]
10000000 loops, best of 3: 0.194 usec per loop
#Yes tuple are faster than list

A simple example of using module timeit ...

# using Python module timeit on a simple statement
# (timeit turns off garbage collection)

import timeit

stmt = "x = (12345679 * 63)//7"

# use n=1000000 passes (this is also the default)
# this way the result will be time per pass in microseconds
elapsed = lambda stmt, n=1000000: timeit.Timer(stmt).timeit(n)

# run and show the timeit test
print( "%s took %0.3f micro-seconds" % (stmt, elapsed(stmt)) )

"""my result with Python 3.1.1 -->
x = (12345679 * 63)//7 took 0.085 micro-seconds
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.