无法在 python 中将数据对象与 timeit.Time 模块一起使用
我正在尝试测量读取然后加密一些数据(独立)所需的时间。但我似乎无法在 timeit 内访问预先创建的数据对象(因为它在自己的虚拟环境中运行)
这工作正常(定时文件读取操作):
t = timeit.Timer("""
openFile = open('mytestfile.bmp', "rb")
fileData = openFile.readlines()
openFile.close()""")
readResult = t.repeat(1,1)
print ("\Finished reading in file")
以下内容不起作用,因为我无法访问“文件数据”对象。我无法从 timeit 函数内部再次创建它,否则会增加整体执行时间。
定时加密操作:
tt = timeit.Timer("""
from Crypto.Cipher import AES
import os
newFile = []
key = os.urandom(32)
cipher = AES.new(key, AES.MODE_CFB)
for lines in fileData:
newFile = cipher.encrypt(lines)""")
encryptResult = tt.repeat(1,1)
I'm trying to measure how long it takes read then encrypt some data (independently). But I can't seem to access the a pre-created data obj within timeit (as it runs in its own virtual environment)
This works fine (timing file read operation):
t = timeit.Timer("""
openFile = open('mytestfile.bmp', "rb")
fileData = openFile.readlines()
openFile.close()""")
readResult = t.repeat(1,1)
print ("\Finished reading in file")
The the below doesn't work because I can't access 'fileData' obj. I can't create it again from inside the timeit function, otherwise it will increase the overall execution time.
timing encrypt operation:
tt = timeit.Timer("""
from Crypto.Cipher import AES
import os
newFile = []
key = os.urandom(32)
cipher = AES.new(key, AES.MODE_CFB)
for lines in fileData:
newFile = cipher.encrypt(lines)""")
encryptResult = tt.repeat(1,1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
运行一次的设置参数
timeit 采用仅从文档
例如:
timeit takes a setup argument that only runs once
from the docs:
for example:
您可以使用
timeit.Timer
类的setup
参数,如下所示:setup
代码仅运行一次。you can use the
setup
parameter of thetimeit.Timer
class like so:The
setup
code is only run once.