无法在 python 中将数据对象与 timeit.Time 模块一起使用

发布于 2024-08-10 19:28:05 字数 641 浏览 11 评论 0原文

我正在尝试测量读取然后加密一些数据(独立)所需的时间。但我似乎无法在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最美不过初阳 2024-08-17 19:28:05

运行一次的设置参数

timeit 采用仅从文档

setup: 执行一次的语句
最初(默认“通过”)

例如:

setup = """
from Crypto.Cipher import AES
import os
newFile = []
fileData = open('filename').read()
"""
stmt = """
key = os.urandom(32)
cipher = AES.new(key, AES.MODE_CFB)
for lines in fileData:
    newFile = cipher.encrypt(lines)"""

tt = timeit.Timer(stmt, setup)
tt.repeat()

timeit takes a setup argument that only runs once

from the docs:

setup: statement to be executed once
initially (default 'pass')

for example:

setup = """
from Crypto.Cipher import AES
import os
newFile = []
fileData = open('filename').read()
"""
stmt = """
key = os.urandom(32)
cipher = AES.new(key, AES.MODE_CFB)
for lines in fileData:
    newFile = cipher.encrypt(lines)"""

tt = timeit.Timer(stmt, setup)
tt.repeat()
狠疯拽 2024-08-17 19:28:05

您可以使用 timeit.Timer 类的 setup 参数,如下所示:

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)""", 
setup = "fileData = open('mytestfile.bmp', 'rb').readlines()")
encryptResult = tt.repeat(1,1)

setup 代码仅运行一次。

you can use the setup parameter of the timeit.Timer class like so:

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)""", 
setup = "fileData = open('mytestfile.bmp', 'rb').readlines()")
encryptResult = tt.repeat(1,1)

The setup code is only run once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文