繁体   English   中英

Python Line Profiler结果不一致

[英]Python line profiler results inconsistent

为什么尽管相同的代码运行两次,但是python代码却以截然不同的速度运行?

我正在对一些简短的Python代码进行分析:

import urllib3

@profile
def download(url, file_path):
    http = urllib3.PoolManager()
    r = http.request("GET", url)
    print("FINISHED GET!")
    print("WRITING TO "+file_path)
    with open(file_path, "wb") as f:
        f.write(r.data)
    r.release_conn()

url = "http://interactivepaper.todayonline.com/jrsrc/260516/260516.pdf"

download(url, "")

测试中

我正在将line_profiler与命令kernprof -l -v test.py 我多次测试了此代码,所有结果均不一致。

测试1:

FINISHED GET!
WRITING TO 
Wrote profile results to test.py.lprof
Timer unit: 1e-06 s

Total time: 44.653 s
File: test.py
Function: download at line 3

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     3                                           @profile
     4                                           def download(url, file_path):
     5         1          273    273.0      0.0      http = urllib3.PoolManager()
     6         1     44652667 44652667.0    100.0      r = http.request("GET", url)
     7         1           37     37.0      0.0      print("FINISHED GET!")
     8         1            4      4.0      0.0      print("WRITING TO "+file_path)
     9         1           29     29.0      0.0      with open(file_path, "wb") as f:
    10                                                   f.write(r.data)
    11                                               r.release_conn()
(There was an IO Error from here onwards as I used an empty string)

测试2(我编辑了代码):

FINISHED GET!
WRITING TO 
Wrote profile results to test.py.lprof
Timer unit: 1e-06 s

Total time: 44.6693 s
File: test.py
Function: download at line 3

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     3                                           @profile
     4                                           def download(url, file_path):
     5         1          186    186.0      0.0      http = urllib3.PoolManager()
     6         1     44669082 44669082.0    100.0      r = http.request("GET", url)
     7         1           42     42.0      0.0      print("FINISHED GET!")
     8         1            4      4.0      0.0      print("WRITING TO "+file_path)

测试3:

FINISHED GET!
WRITING TO 
Wrote profile results to test.py.lprof
Timer unit: 1e-06 s

Total time: 4.53504 s
File: test.py
Function: download at line 3

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     3                                           @profile
     4                                           def download(url, file_path):
     5         1          262    262.0      0.0      http = urllib3.PoolManager()
     6         1      4534736 4534736.0    100.0      r = http.request("GET", url)
     7         1           37     37.0      0.0      print("FINISHED GET!")
     8         1            4      4.0      0.0      print("WRITING TO "+file_path)

这是我感到困惑的部分。 最初需要44秒才能运行的过程现在需要4秒即可运行。 我还指出,每当我编辑文件时,都需要很长时间才能再次运行。 这是另外三个测试,证明了我的观点:

编辑后的第一个测试:

Wrote profile results to test.py.lprof
Timer unit: 1e-06 s

Total time: 49.7018 s
File: test.py
Function: download at line 3

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     3                                           @profile
     4                                           def download(url, file_path):
     5         1          187    187.0      0.0      http = urllib3.PoolManager()
     6         1     49701585 49701585.0    100.0      r = http.request("GET", url)

编辑后的第二项测试:

Timer unit: 1e-06 s

Total time: 9.10985 s
File: test.py
Function: download at line 3

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     3                                           @profile
     4                                           def download(url, file_path):
     5         1          185    185.0      0.0      http = urllib3.PoolManager()
     6         1      9109665 9109665.0    100.0      r = http.request("GET", url)

编辑后的第三项测试(与第二项测试类似):

Wrote profile results to test.py.lprof
Timer unit: 1e-06 s

Total time: 12.9593 s
File: test.py
Function: download at line 3

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     3                                           @profile
     4                                           def download(url, file_path):
     5         1          189    189.0      0.0      http = urllib3.PoolManager()
     6         1     12959072 12959072.0    100.0      r = http.request("GET", url)

主要区别在于以下代码行:

r = http.request("GET", url)

在此行中,您尝试访问远程Web服务器。

以下原因可能导致对Web服务器的访问时间不同:

1)缓存

2)网络负载

3)远程服务器负载

暂无
暂无

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

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