Time execution of the python-code

import time
start = time.time()
for i in [0, 100]:
    i = i + 1
stop_point_somecode = time.time()
for j in [1, 1000]:
    j = j + 1
stop_point_somecode_2 = time.time()
  
print "Runtime somecode %s" % (stop_point_somecode - start)
print "All runtime %s" % (stop_point_somecode_2 - start)

To calculate, you can create a decorator:

import time, math

def time_execution(f):
   def tmp(*args, **kwargs):
       t = time.time()
       res = f(*args, **kwargs)
       print "time: %f" % (time.time()-t)
       return res
 
   return tmp

@time_execution
def some_func(value):
    math.log10(value)


if __name__ == "__main__":
    some_func(76)

 

comments are disabled for this article
Search
Popular Posts
Subscribe
{{post}}