Time a function: Difference between revisions

m (→‎{{header|Phix}}: added elapsed() example)
Line 1,912:
 
=={{header|Python}}==
{{incorrect|Python|cannot import pow}}
Cannot run this on 2.7 or 3.7.3. Added () to print for the latter, also fixed an
IndentationError: unindent does not match any outer indentation level on nothing/identity, but:<br>
ImportError: cannot import name 'pow' from '__main__' (test.py)
 
Given ''function'' and ''arguments'' return a time (in microseconds) it takes to make the call.
 
Line 1,931 ⟶ 1,936:
raise
 
def nothing(): pass
def identity(x): return x</lang>
 
===Example===
>>> print (usec(nothing, []))
1.7
>>> print (usec(identity, [1]))
2.2
>>> print (usec(pow, (2, 100)))
3.3
>>> print ([usec(qsort, (range(n),)) for n in range(10)])
[2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0]
using ''qsort()'' from [[Quicksort#Python|Quicksort]]. Timings show that the implementation of ''qsort()'' has quadratic dependence on sequence length ''N'' for already sorted sequences (instead of ''O(N*log(N))'' in average).
7,820

edits