2

我有一个计时模块:

start_timing = time.time()
#stuff happening here    
report_timing["INT_MODULE"] = time.time()- start_timing

如果我使用str(report_timing["INT_MODULE"])我得到的是很长的:0.000441074371338

是否有一种标准方法可以将此浮点数舍入0.0004或以其他方式表示为更少的数字440 microseconds,换句话说,一种转换时间的标准方法?

4

3 回答 3

1

report_timing["INT_MODULE"] = int((time.time() - start_timing)*1000.0) / 1000

于 2012-10-30T09:45:41.990 回答
1

您可以尝试一些字符串格式:

In [1]: strs=0.000441074371338

In [2]: "{0:e}".format(strs)
Out[2]: '4.410744e-04'

In [3]: "{0:.4f}".format(strs)
Out[3]: '0.0004'

In [9]: "{0:.0f} us".format(strs*1000*1000)
Out[9]: '441 us'
于 2012-10-30T09:47:08.263 回答
0

您可以使用默认的内置函数对 long 进行舍入; http://docs.python.org/2/library/functions.html#round

至于转成微秒,恐怕也无能为力了。

于 2012-10-30T09:47:00.513 回答