我想要的是开始在我的代码中的某个地方计算时间,然后获得通过的时间,以测量执行少量功能所花费的时间.我认为我使用的是timeit模块错误,但文档对我来说只是让人困惑.
import timeit
start = timeit.timeit()
print("hello")
end = timeit.timeit()
print(end - start)
Run Code Online (Sandbox Code Playgroud) 使用python中的Time模块可以测量经过的时间吗?如果是这样,我该怎么做?
我需要这样做,以便如果光标已在窗口小部件中持续一段时间,则会发生事件.
我想测量在Python程序中评估代码块所花费的时间,可能在用户CPU时间,系统CPU时间和已用时间之间进行分离.
我知道timeit模块,但是我有很多自编函数,在设置过程中传递它们并不容易.
我宁愿有一些可以使用的东西,如:
#up to here I have done something....
start_counting() #or whatever command used to mark that I want to measure
#the time elapsed in the next rows
# code I want to evaluate
user,system,elapsed = stop_counting() #or whatever command says:
#stop the timer and return the times
Run Code Online (Sandbox Code Playgroud)
用户和系统CPU时间不是必需的(虽然我想测量它们),但是对于经过的时间我希望能够做这样的事情,而不是使用复杂的命令或模块.
所以在Java中,我们可以做如何测量函数执行所花费的时间
但它是如何在python中完成的?要测量代码行之间的时间开始和结束时间?这样做的东西:
import some_time_library
starttime = some_time_library.some_module()
code_tobe_measured()
endtime = some_time_library.some_module()
time_taken = endtime - starttime
Run Code Online (Sandbox Code Playgroud) 我只想尝试一段代码.伪代码看起来像:
start = get_ticks()
do_long_code()
print "It took " + (get_ticks() - start) + " seconds."
Run Code Online (Sandbox Code Playgroud)
这看起来如何用Python?
更具体地说,我如何得到午夜以来的滴答数(或者Python组织那个时间)?
我是Python的新手,并且对日期/时间文档感到困惑.我想计算执行计算所需的时间.
在java中,我会写:
long timeBefore = System.currentTimeMillis();
doStuff();
long timeAfter = System.currentTimeMillis();
elapsed time = timeAfter - timeBefore;
Run Code Online (Sandbox Code Playgroud)
我确信它在Python中更容易.有人可以帮忙吗?
我在StackOverflow上找到了这个问题和答案.
Python - time.clock()与time.time() - 准确度?
这是我正在尝试运行的一些代码:
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__ …Run Code Online (Sandbox Code Playgroud) 我最近一直在构建一个错误记录应用程序,并且正在为一种准确的时间戳记输入数据.当我准确地说我的意思是每个时间戳应该相对于彼此准确(不需要同步到原子钟或类似的东西).
我一直在使用datetime.now()作为第一次尝试,但这并不完美:
>>> for i in range(0,1000):
... datetime.datetime.now()
...
datetime.datetime(2008, 10, 1, 13, 17, 27, 562000)
datetime.datetime(2008, 10, 1, 13, 17, 27, 562000)
datetime.datetime(2008, 10, 1, 13, 17, 27, 562000)
datetime.datetime(2008, 10, 1, 13, 17, 27, 562000)
datetime.datetime(2008, 10, 1, 13, 17, 27, 578000)
datetime.datetime(2008, 10, 1, 13, 17, 27, 578000)
datetime.datetime(2008, 10, 1, 13, 17, 27, 578000)
datetime.datetime(2008, 10, 1, 13, 17, 27, 578000)
datetime.datetime(2008, 10, 1, 13, 17, 27, 578000)
datetime.datetime(2008, 10, 1, 13, 17, 27, 609000) …Run Code Online (Sandbox Code Playgroud) 我正在尝试计算numpy数组中的唯一值.
import numpy as np
from collections import defaultdict
import scipy.stats
import time
x = np.tile([1,2,3,4,5,6,7,8,9,10],20000)
for i in [44,22,300,403,777,1009,800]:
x[i] = 11
def getCounts(x):
counts = defaultdict(int)
for item in x:
counts[item] += 1
return counts
flist = [getCounts, scipy.stats.itemfreq]
for f in flist:
print f
t1 = time.time()
y = f(x)
t2 = time.time()
print y
print '%.5f sec' % (t2-t1)
Run Code Online (Sandbox Code Playgroud)
我起初找不到内置函数,所以我写道getCounts(); 然后我发现我scipy.stats.itemfreq以为我会用它来代替.但它很慢!这是我在电脑上得到的.与这么简单的手写功能相比,为什么这么慢?
<function getCounts at 0x0000000013C78438>
defaultdict(<type 'int'>, {1: 19998, 2: 20000, 3: …Run Code Online (Sandbox Code Playgroud)