0
0

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 5 years have passed since last update.

ifを使う場合とwhileを使う場合

Last updated at Posted at 2015-06-22

コメントを頂いたので、ソースを見なおしてみたら、
whileの方に余計な処理が入り込んでいて遅くなっていただけだった。実質的には、ほぼ変わらず、whileの方がコンマ遅れで終了する程度のズレだった。


.py
b = time.clock()
execute() # 処理
a = time.clock() - b

修正後のソース

.py

import time

def hoge_while(h):
    while h % 15 == 0: return "KinokoTakenoko"
    while h % 3 == 0: return "Kinoko"
    while h % 5 == 0: return "Takenoko"
    return ""

def hoge_if(h):
    result = ""
    if h % 3 == 0: result += "Kinoko"
    if h % 5 == 0: result += "Takenoko"
    return result

def loop(hoge_list):
    i = 1
    while i <= 10000000:
        i, hoge_list[i-1]
        i += 1

before = time.clock()
loop([hoge_if(i) for i in range(1, 10000001)])
print time.clock() -before
before = time.clock()
loop([hoge_while(i) for i in range(1, 10000001)])
print time.clock() -before
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?