0
1

最もよく起こりうることを先に判定せよ

Posted at
if condition:
    continue
else:
    something()

の意味と,「最もよく起こりうることを先に判定せよ」というのは違うのではないか。
確かに以下のような場合には  i < 0.8 を先に判定するほうが良いが,それとこれとは違う。

#---
import numpy as np
from time import time
n = 100000000
x = np.random.rand(n)

s = time()
s1, s2, s3 = 0, 0, 0
for i in x:
   if i < 0.8:
       s1 += 1
   elif i < 0.9:
       s2 += 1
   else:
       s3 += 1
print(s1, s2, s3, time() - s)

       
79997428 9999472 10003100 10.772604942321777
s = time()
s1, s2, s3 = 0, 0, 0
for i in x:
    if i > 0.9:
        s3 += 1
    elif i > 0.8:
        s2 += 1
    else:
        s1 += 1
print(s1, s2, s3, time() - s)

79997428 9999472 10003100 11.210047960281372
0
1
0

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