The example in this article describes how Python executes the specified function at timed intervals. Share to everyone for your reference. The implementation method is as follows:
# time a function using Time.time () and the A @ function decorator# tested with PYTHON24 vegaseat 21aug2005import Timede F print_timing (func): Def wrapper (*arg): T1 = time.time () res = func (*arg) t2 = time.time () print '%s took%0 .3f ms '% (Func.func_name, (T2-T1) *1000.0) return res return wrapper# declare the @ decorator just before the function , invokes Print_timing () @print_timingdef getprimelist (n): "" "returns a list of prime numbers from 2 to < n using a Si Eve algorithm "" "If n < 2:return [] if n = = 2:return [2] # do-odd numbers starting at 3 S = Range (3, n+1, 2) # n**0.5 May is slightly faster than MATH.SQRT (n) mroot = n * * 0.5 half = Len (s) 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]if __name__ = = "__main__": print "prime numbers from 2 to <10,000,000 using a sieve al Gorithm "primelist = geTprimelist (10000000) time.sleep (2.5) "" "My output-->prime numbers from 2 to <10,000,000 using a sieve ALGORITHMGETP Rimelist took 4750.000 ms "" "
Hopefully this article will help you with Python programming.