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"

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

timeit uses time.clock() on Windows. 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().
@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.
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.
@J.F.Sebastian: That's true. I've clarified my answer.
Now this is a good answer, not that "Google it" elitist "answer" given before.
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/

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.