簡體   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