在Python 2中,floor()
返回了一个浮点值.虽然对我来说不是很明显,但我找到了一些解释,说明为什么floor()
返回浮点数可能是有用的(对于像float('inf')
和的情况float('nan')
).
但是,在Python 3中,floor()
返回整数(并返回前面提到的特殊情况的溢出错误).
那么现在int()
和floor()
现在有什么不同呢?
有没有办法查看脚本在 VS Code 中执行/完成需要多长时间?
我正在寻找这样的消息:
Program finished in 30ms
Run Code Online (Sandbox Code Playgroud) 我在 python 代码中遇到错误:“从时间导入时钟”导入错误:无法从“时间”(未知位置)导入名称“时钟”
这在 python 3.8 中不起作用有人可以帮忙吗
我希望能够使用Python日志记录工具在我的代码中进行简单一致的日志记录.
我能够做到以下几点:
don't want to add everywhere
在每个函数定义中添加相同的代码片段来定义日志记录参数(如下所示).log.info(...)
etc构造在我在项目层次结构中定义的任何函数中工作.什么不起作用/我不知道该怎么做:
@log
在我编写的每个现有/新模块中定义相同的装饰器.# don't want to add everywhere
FORMAT = '%(asctime)s - %(name)-20s - %(levelname)-5s - %(message)s'
LEVEL = logging.DEBUG
logging.basicConfig(format=FORMAT, level=LEVEL)
log = logging.getLogger(__name__)
Run Code Online (Sandbox Code Playgroud)
我的Flask项目的示例代码:
# app/__init__.py
from a import b # various other imports required for app
import logging
FORMAT = '%(asctime)s - %(name)-20s - %(levelname)-5s - %(message)s'
LEVEL = logging.DEBUG
logging.basicConfig(format=FORMAT, level=LEVEL)
log = logging.getLogger(__name__)
# ... various other app init code …
Run Code Online (Sandbox Code Playgroud) 我使用time.clock
和在Ubuntu上定时了一段python代码time.time
:
clock elapsed time: 8.770 s
time elapsed time: 1.869 s
Run Code Online (Sandbox Code Playgroud)
我知道time.time使用系统时间和time.clock使用处理器时钟.当time.time给出比time.clock更长的经过时间时,对我来说很有意义:处理器在整个时间内都没有活动(例如,调用时间time.sleep
).
但是为什么/何时处理器时钟会比系统时间大得多?
附录
我做了一个粗略的测试,使用标准映射计算相同的函数,使用进程池映射和线程池映射.可以理解,进程池速度更快,线程池更慢.更有趣的是:时钟时序小于处理器池的时间,但线程池中的时间更长.
同样,我理解为什么处理器池的时钟时序较少:假设主进程没有做太多事情,只是等待池进程完成.但是为什么线程池的时钟时间更长?任何见解?
结果:
map
time 1738.8
clock 1739.6
mp pool
time 580.1
clock 15.9
thread pool
time 3455.3
clock 5378.9
Run Code Online (Sandbox Code Playgroud)
码:
from time import clock, sleep, time
from multiprocessing.pool import ThreadPool
from multiprocessing import Pool
import random
def f(i):
x = [random.random() for j in range(100000)]
return x[i]
def t(fn):
t0, c0 = time(), …
Run Code Online (Sandbox Code Playgroud) 考虑一个非常简单的计时器
start = time.time()
end = time.time() - start
while(end<5):
end = time.time() - start
print end
Run Code Online (Sandbox Code Playgroud)
这个计时器有多精确?我的意思是与实时时钟相比,这个是如何同步和实时的?
现在是真正的问题;
使用Python可以精确测量的最小时间尺度是多少?