2016-05-26 4 views
0

同じコードが2回実行されているにもかかわらず、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, "") 

テスト

私はコマンドkernprof -l -v test.pyline_profilerを使用しています。私はこのコードを複数回テストしたが、すべての結果に一貫性がなかった。

試験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) 

これは私が混乱見つかった部分です。 最初に実行するのに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) 

サード編集後の試験(第二のテストとその類似の):

ここに私のポイントを証明する3つのテストがあります
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

主な相違点は、次のコード行です。

あなたは、リモートのWebサーバにアクセスしようとしている。このラインで
r = http.request("GET", url) 

1)キャッシュ

2)ネットワーク負荷

3)リモートサーバーの負荷

次の理由は、Webサーバーへの異なるアクセス時間となる可能性があります

関連する問題