2016-05-26 105 views
0

儘管相同的代碼運行兩次,爲什麼Python代碼以極大不同的速度運行?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(I編輯的代碼):

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) 

這是我發現令人困惑的部分。 最初花費44s運行的過程現在需要4s才能運行。我還注意到,無論何時編輯文件,都需要很長時間才能再次運行。這裏有三個以上的測試證明了我的觀點:

編輯後的第一個測試:編輯後

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) 

回答

1

主要區別在於以下代碼行:

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

在這一行中,您嘗試訪問遠程Web服務器。

以下原因可能導致不同的訪問時間Web服務器:

1)高速緩存

2)網絡負載

3)遠程服務器負載