用-B进行速度优化; __pycache__仍在某处生成?

时间:2015-02-25 14:46:19

标签: python python-3.x

请注意,问题已根据评论进行了更新,并且在此工作中发现了加速的原因,因为它经常发生: - )

对于优化Python 3.4加载时间的实验,使用Python -B选项“不要在导入时写.py [co]文件”以避免本地__pycache__文件,我创建了这段代码,因为我发现大部分时间花在了import s:

import datetime

modules = ['my_module_1', 'my_module_2']

time_sum = datetime.timedelta()  # Zero delta for start
for module in modules:
    time_begin = datetime.datetime.now()
    __import__(module)
    time_sum += datetime.datetime.now() - time_begin
print('{:.<10s}: {:.03f} s'.format('TOTAL', time_sum.total_seconds()))

使用-B选项,我看到Win7 cmd(网络驱动器),Win7 Cygwin(网络驱动器)和CentOS 6.3(本地驱动器)的TOTAL运行时,对于每个平台的新副本,如下:

  1. 0.763 s,0.652 s,0.070 s
  2. 0.655 s,0.586 s,0.069 s
  3. 0.583s,0.596s,0.069s
  4. 所以看起来即使没有本地__pycache__文件,也会从第一次运行到后续运行进行一些优化,但这可能是因为缓存了通过网络驱动器检索到的文件,正如@PeterSutton指出的那样评价。

    如果没有-B选项,则结果为:

    1. 0.853 s,0.833 s,0.073 s
    2. 0.578s,0.590s,0.052s
    3. 0.567s,0.569s,0.052s
    4. 因此,这清楚地显示了本地__pycache__文件的速度提升。

      Python在其他地方(在某个位置临时位置)生成__pycache__文件以获取特定位置的源代码吗? 不,但文件在内核中缓冲。

0 个答案:

没有答案