Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to time execution time of a batch of code in Python?

Tags:

python

time

I found this question and answer here on StackOverflow.

Python - time.clock() vs. time.time() - accuracy?

Here is some code I'm trying to run:

import sys
import time
import timeit

if (len(sys.argv) > 1):
  folder_path = sys.argv[1]
  if not os.path.isdir(folder_path):
    print "The folder you provided doesn't exist"    
  else:
    print_console_headers()    
    rename_files_to_title_case(folder_path)
    #start the timer.
    #do some freaky magic here.
    #end the timer.

else:
  print "You must provide a path to a folder."

def print_console_headers():
  print "Renaming files..."
  print "--------------------"
  return

def rename_files_to_title_case():  
  """this is just for testing purposes"""  
  L = []
  for i in range(100):
      L.append(i)

if __name__ == '__main__':
    from timeit import Timer
    t = Timer("test()", "from __main__ import test")
    print t.timeit()

How would I give timeit a function with a parameter that's been saved elsewhere?

This is something I've written in Ruby that gave me results in clean code, maybe this helps for suggestions.

start_time = Time.now

folder_path = ARGV[0]
i = 0
Dir.glob(folder_path + "/*").sort.each do |f|
    filename = File.basename(f, File.extname(f))
    File.rename(f, folder_path + "/" + filename.gsub(/\b\w/){$&.upcase} + File.extname(f))
    i += 1
end

puts "Renaming complete."
puts "The script renamed #{i} file(s) correctly."
puts "----------"
puts "Running time is #{Time.now - start_time} seconds"
like image 497
Only Bolivian Here Avatar asked Jan 17 '12 02:01

Only Bolivian Here


People also ask

How do you calculate execution time?

1) Create a loop around whatneeds to be measured, that executes 10, 100, or 1000 times or more. Measure execution time to the nearest 10 msec. Then divide that time bythe number of times the loop executed. If the loop executed 1000 timesusing a 10 msec clock, you obtain a resolution of 10 µsec for theloop.

What is execution time in Python?

Python timeit module runs your snippet of code n number of times (the default value is, 1000000) so that you get the statistically most relevant measurement of code execution time.


2 Answers

I would use a timing decorator and place the code you want to time into a function.

import time

def timeit(f):

    def timed(*args, **kw):

        ts = time.time()
        result = f(*args, **kw)
        te = time.time()

        print 'func:%r args:[%r, %r] took: %2.4f sec' % \
          (f.__name__, args, kw, te-ts)
        return result

    return timed

Using the decorator is easy either use annotations.

@timeit
def compute_magic(n):
     #function definition
     #....

Or re-alias the function you want to time.

compute_magic = timeit(compute_magic)

My blog post here has more information. http://blog.mattalcock.com/2013/2/24/timing-python-code/

like image 89
Matt Alcock Avatar answered Sep 19 '22 03:09

Matt Alcock


This is how I typically write time measurement code in Python:

start_time = time.time()

# ... do stuff

end_time = time.time()
print("Elapsed time was %g seconds" % (end_time - start_time))

As mentioned in the post you linked to, time.clock() is not appropriate for measuring elapsed time since it reports the amount of CPU time used by your process only (at least on Unix systems). Using time.time() is cross-platform and reliable.

like image 41
Greg Hewgill Avatar answered Sep 17 '22 03:09

Greg Hewgill