【发布时间】:2012-01-17 02:47:17
【问题描述】:
我在 StackOverflow 上找到了这个问题和答案。
Python - time.clock() vs. time.time() - accuracy?
这是我正在尝试运行的一些代码:
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()
如何给 timeit 一个带有已保存在别处的参数的函数?
这是我用 Ruby 编写的东西,它给我的结果是干净的代码,也许这有助于提供建议。
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"
【问题讨论】: