13

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"
Community
  • 1
  • 1
Only Bolivian Here
  • 35,719
  • 63
  • 161
  • 257

4 Answers4

35

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.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • [`timeit` uses `time.clock()` on Windows](http://hg.python.org/cpython/file/2.7/Lib/timeit.py#l70). `from timeit import default_timer as timer; start_time = timer(); ...` allows to switch between `time.time()` on *nix and `time.clock()` on Windows automatically or later It might use [`time.walltime()`](http://bugs.python.org/issue10278). – jfs Jan 17 '12 at 05:48
  • @J.F.Sebastian: I don't think the OP is looking for the functionality of `time.clock()`, which is why I don't think `timeit` is an appropriate choice. – Greg Hewgill Jan 17 '12 at 05:59
  • On Windows `time.clock()` measures walltime i.e., your remark: *"it reports the amount of CPU time used by your process only."* might be incorrect. – jfs Jan 17 '12 at 07:03
  • @J.F.Sebastian: That's true. I've clarified my answer. – Greg Hewgill Jan 17 '12 at 07:08
  • 2
    Now *this* is a good answer, not that "Google it" elitist "answer" given before. – Only Bolivian Here Jan 17 '12 at 15:01
16

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/

Matt Alcock
  • 12,399
  • 14
  • 45
  • 61
4

A fun way to time functions is by using decorators and wrapper functions. One of the functions I use for this is:

import time

def print_timing(func):
    def wrapper(*arg):
        t1 = time.time()
        res = func(*arg)
        t2 = time.time()
        string = '| %s took %0.3f ms |' % (func.func_name, (t2-t1)*1000.0)
        print
        print '-'*len(string)
        print string
        print '-'*len(string)
        print
        return res
    return wrapper

Any function decorated by @print_timing, will print print the time it took to stdout

@print_timing
def some_function(text):
    print text

This makes it very convenient to time specific functions.

2

If you need to measure a timing of specific lines of code within the function, when decorator can't help there is a solution

import time
from time import sleep

class TT(object):
    def __init__(self):
        self.start = time.time()
    def __str__(self):
        return str(time.time() - self.start)


timeit = TT()

sleep(1)
print timeit

sleep(2)
print timeit

will print:

1.0
3.00
DmitrySemenov
  • 9,204
  • 15
  • 76
  • 121