-1
import time
word = {"success":0, "desire":0, "effort":0, ...}
def cleaner(x):
    dust = ",./<>?;''[]{}\=+_)(*&^%$#@!`~"
    for letter in x:
        if letter in dust:
            x = x[0:x.index(letter)]+x[x.index(letter)+1:]
        else:
            pass
    return x #alhamdlillah it worked 31.07.12
print "input text to analyze"
itext = cleaner(raw_input()).split()
t = time.clock()
for iword in itext:
    if iword in word:
        word[iword] += 1
    else:
        pass
print t
print len(itext)

每次我调用代码时,t 都会增加。谁能解释这背后的基本概念/原因。也许在系统过程方面?非常感谢你们,编程小伙子们。

4

5 回答 5

10

因为每次运行脚本时都会打印出当前时间

时间就是这样运作的,它不断地前进。

于 2012-08-09T03:09:03.703 回答
3

如果要测量for循环所用的时间(从第一次调用到time.clock()结束),请打印出时间

print time.clock() - t
于 2012-08-09T03:10:27.623 回答
0

您正在打印当前时间......当然,每次运行代码时它都会增加。

从 python 文档中time.clock()

在 Unix 上,以浮点数形式返回当前处理器时间,以秒为单位。精度,实际上是“处理器时间”含义的定义,取决于同名 C 函数的精度,但无论如何,这是用于对 Python 或计时算法进行基准测试的函数。

在 Windows 上,此函数根据 Win32 函数 QueryPerformanceCounter() 返回自第一次调用此函数以来经过的挂钟秒数,作为浮点数。分辨率通常优于一微秒。

于 2012-08-09T03:12:23.023 回答
0

time.clock()返回自进程创建以来经过的CPU 时间。CPU 时间基于 CPU 在进程上下文中花费的周期数。它在进程的生命周期内是一个单调函数,即如果您time.clock()在同一执行过程中多次调用,您将得到一个递增数字的列表。两次连续调用之间的差异clock()可能小于或更多,这取决于 CPU 没有以 100% 运行(例如,有一些等待 I/O)或者如果您有一个多线程程序消耗超过 100% 的 CPU 时间(例如,多核 CPU 有 2 个线程,每个线程使用 75% -> 你将获得 150% 的挂钟时间)。但是如果你打电话clock()在一个进程中一次,然后再次重新运行程序,如果在新进程中处理输入所需的时间更少,您可能会得到比以前更低的值。

您应该做的是使用time.time()which 返回具有小数(亚秒)精度的当前 Unix 时间戳。您应该在处理开始之前调用一次,之后调用一次,然后减去两个值,以计算两次调用之间经过的挂钟时间。

请注意,在 Windows 上,会time.clock()返回自进程启动以来经过的挂钟时间。这就像time.time()在脚本开始时立即调用,然后从后面的调用中减去值time.time()

于 2012-08-09T07:03:48.390 回答
0

有一个非常好的名为jackedCodeTimerPy的库,它比 time 模块工作得更好。它还具有一些巧妙的错误检查功能,因此您可能想尝试一下。

使用 jackedCodeTimerPy,您的代码应如下所示:

# import time
from jackedCodeTimerPY import JackedTiming
JTimer = JackedTiming()

word = {"success":0, "desire":0, "effort":0}
def cleaner(x):
    dust = ",./<>?;''[]{}\=+_)(*&^%$#@!`~"
    for letter in x:
        if letter in dust:
            x = x[0:x.index(letter)]+x[x.index(letter)+1:]
        else:
            pass
    return x #alhamdlillah it worked 31.07.12
print "input text to analyze"
itext = cleaner(raw_input()).split()

# t = time.clock()
JTimer.start('timer_1')
for iword in itext:
    if iword in word:
        word[iword] += 1
    else:
        pass
# print t
JTimer.stop('timer_1')
print JTimer.report()
print len(itext)

它提供了非常好的报告,例如

label            min          max         mean        total    run count
-------  -----------  -----------  -----------  -----------  -----------
imports  0.00283813   0.00283813   0.00283813   0.00283813             1
loop     5.96046e-06  1.50204e-05  6.71864e-06  0.000335932           50

我喜欢它如何为您提供有关它的统计信息以及计时器运行的次数。

使用起来很简单。如果我想测量 for 循环中的时间代码,我只需执行以下操作:

from jackedCodeTimerPY import JackedTiming
JTimer = JackedTiming()

for i in range(50):
  JTimer.start('loop')  # 'loop' is the name of the timer
  doSomethingHere = 'This is really useful!'
  JTimer.stop('loop')
print(JTimer.report())  # prints the timing report

您还可以同时运行多个计时器。

JTimer.start('first timer')
JTimer.start('second timer')
do_something = 'amazing'
JTimer.stop('first timer')

do_something = 'else'
JTimer.stop('second timer')

print(JTimer.report())  # prints the timing report

回购中有更多使用示例。希望这可以帮助。

https://github.com/BebeSparkelSparkel/jackedCodeTimerPY

于 2016-10-14T23:24:17.260 回答