相关疑难解决方法(0)

如何在python中获得单调持续时间?

我想记录真实壁挂时间有多长.目前我这样做:

startTime = time.time()
someSQLOrSomething()
print "That took %.3f seconds" % (time.time() - startTime)
Run Code Online (Sandbox Code Playgroud)

但是如果在SQL查询(或其他任何东西)运行时调整时间,则会失败(产生不正确的结果).

我不想只是对它进行基准测试.我想在实时应用程序中记录它,以便查看实时系统的趋势.

我想要像clock_gettime(CLOCK_MONOTONIC,...)这样的东西,但是在Python中.并且最好不必编写调用clock_gettime()的C模块.

python linux benchmarking clock

55
推荐指数
3
解决办法
2万
查看次数

计时器显示消极时间已过

我正在使用一个非常简单的代码来为for语句中的每个循环计时.它看起来像这样:

import time

for item in list_of_files:

    # Start timing this loop.
    start = time.clock()

    # Do a bunch of stuff.

    # Get time elapsed in seconds.
    elapsed = time.clock() - start

    # Get minutes and seconds.
    m, s = divmod(elapsed, 60)

    # Print result.
    print 'End of analysis for %s in %dm %02ds.\n'% (item, m, s)
Run Code Online (Sandbox Code Playgroud)

这大部分时间会产生正确的输出,9m 52s但有时会出现大的循环(需要一些时间),我会得到负面结果-53m 17s.

我无法确定一个明确的限制,其中结果开始显示为负值,但它显然只发生在> 20分钟的经过时间,但不是每个花费超过20分钟的循环(即:它不一致).

我的代码中有什么东西可以计算导致这种情况的每个循环吗?

我正在使用Spyder我的IDE.我比较了time.clock()表现得如何time.time(),这就是我所看到的:

>>> …
Run Code Online (Sandbox Code Playgroud)

python time

8
推荐指数
1
解决办法
2788
查看次数

使用标准库在Python中检索壁垒时间?

如何使用标准库在Python中检索挂壁时间

这个问题,这个问题表明在Linux上类似clock_gettime(CLOCK_MONOTONIC_RAW)/proc/uptime最合适的东西.在Windows上,time.clock()具有所需的效果.

我会使用time.time(),但不保证函数单调(和线性)返回增加时间值.

python time clock standard-library wall-time

7
推荐指数
1
解决办法
1753
查看次数

Pycurl:如何确定请求的持续时间

我的任务是定期向服务器发出请求并将该请求的时间写入数据库。我使用 Python 2.7.3、cURL 和 pycurl 作为 cURL 的包装器。我确实这样要求:

import pycurl
c = pycurl.Curl()
c.setopt(c.URL, "http://google.com/")
c.setopt(c.VERBOSE, True) # to see request details
c.perform()
Run Code Online (Sandbox Code Playgroud)

但我如何确定这个请求的时间呢?

UPD1

好的,我明白,我应该采用 2 个时间戳,它们之间的差异将是请求的持续时间。但我面临一些问题:

当我执行时:

import pycurl, time
c = pycurl.Curl()
c.setopt(c.URL, "http://google.com/")
c.setopt(c.VERBOSE, True) # to see request details
ts = time.clock()
c.perform()
print time.clock() - ts
Run Code Online (Sandbox Code Playgroud)

我得到 0.0 o(有时)0.01。这是错误的 diff,因为我在 python shell 中执行此命令,并且在执行之后ts = time.clock()和执行之前还剩下一些时间print time.clock() - ts,所以 diff 应该约为 3 秒。

我在 Virtualbox 上安装的 Ubuntu 服务器 12.04 上得到此输出。Virtualbox 安装在 …

python time curl pycurl

2
推荐指数
1
解决办法
3431
查看次数

标签 统计

python ×4

time ×3

clock ×2

benchmarking ×1

curl ×1

linux ×1

pycurl ×1

standard-library ×1

wall-time ×1