0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Python備忘録] 各種時間検証・While文の1回目が異常に遅い

Last updated at Posted at 2019-11-30

環境

Python 3.6.8 :: Anaconda, Inc.
pyenv + virtualenv利用
Ubuntu 18.04(ノートPC,サーバともに)

print文は当然遅い

print文はまじで遅い

now = time.perf_counter() 
print(now) 
post = time.perf_counter() 
post2 = time.perf_counter() 
print(post-now) 
print(post2-post)

サーバにて強めのCPUで回した結果

3076898.47890643
0.00023056520149111748
3.0534807592630386e-05

ノートPC(core i5 vPro 8th Gen)で回した結果

144989.874304118
0.0001471959985792637
4.1409977711737156e-05

100~200マイクロ秒のオーダーで時間がかかるんですね

while文の1回目が異常に遅い

While文に入るときの初回って異常に遅いんですね…オーダーが数桁違います…1~100マイクロ秒は決して無視できない(こともたまにあるの)です…

import time

now = time.perf_counter()  
print(now)  
print("1: ", time.perf_counter() - now)  
now = time.perf_counter()  
i = 0
print("2: ", time.perf_counter()-now)  
now = time.perf_counter()  
while(True):  
    print("3: ", time.perf_counter()-now)  
    i+ = 1  
    now = time.perf_counter()  
    if i > 10:  
        break

サーバにて強めのCPUで回した結果

3038070.24794605
1:  0.00013073207810521126
2:  4.219589754939079e-05
3:  4.715612158179283e-05
3:  4.00003045797348e-07
3:  2.752058207988739e-07
3:  2.169981598854065e-07
3:  2.1094456315040588e-07
3:  2.1280720829963684e-07
3:  2.05356627702713e-07
3:  2.1373853087425232e-07
3:  2.039596438407898e-07
3:  2.100132405757904e-07
3:  2.9010698199272156e-07

ノートPC(core i5 vPro 8th Gen)で回した結果

105718.03308281
1:  0.00013153199688531458
2:  0.00010217200906481594
3:  0.00010353099787607789
3:  7.040071068331599e-07
3:  3.370078047737479e-07
3:  3.1400122679769993e-07
3:  3.2399839255958796e-07
3:  3.2500247471034527e-07
3:  6.600021151825786e-07
3:  3.949971869587898e-07
3:  3.789900802075863e-07
3:  3.180030034855008e-07
3:  4.6400236897170544e-07
0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?