简体   繁体   English

有人将如何在Python中对io.open()进行基准测试?

[英]How would someone benchmark io.open() in Python?

Let's say I want to see if reading a file using os.open() is actually faster than io.open() . 假设我想看看是否使用os.open()读取文件实际上比io.open()更快。 I know that one returns a file descriptor and the other one returns an object, but the contents of a file can be read both ways (note that I'm interested in reading binary data ( b or os.O_BINARY ). Any thoughts? 我知道一个返回一个文件描述符,另一个返回一个对象,但是文件的内容可以双向读取(请注意,我对读取二进制数据( bos.O_BINARY )感兴趣。

https://docs.python.org/2/library/timeit.html is the usual way of doing these kind of microbenchmarks. https://docs.python.org/2/library/timeit.html是执行此类微基准测试的常用方法。

from timeit import timeit
timeit("f=os.open('testfile', os.O_RDONLY);os.read(f, 999);os.close(f)",
       setup="import os")

Should get you started. 应该让您开始。 You may need to try with turning the garbage collector on and off and see if there's a difference too. 您可能需要尝试打开和关闭垃圾收集器,看看是否也有区别。 But that's all documented in the timeit docs. 但这全部记录在timeit文档中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM