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"
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.
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.
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/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With