When using if and when using while



b = time.clock()
execute() #processing
a = time.clock() - b

Modified source


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

Recommended Posts

When using if and when using while
Precautions when using codecs and pandas
Swap 1 and 2 without using an if statement
Summary when using Fabric
Precautions when using Chainer
(Personal) points when using ctypes
When using optparse with iPython
File structure when using serverless-python-requirements
This and that using reflect
Try using pytest-Overview and Samples-
Use configparser when using API
Small speedup when using pytorch
[Python] Error and solution memo when using venv with pyenv + anaconda
A memorandum when using beautiful soup
Variable scope when using internal functions
Proxy measures when using WEB API
Benefits and examples of using RabbitMq
Precautions when using pit in Python
Summary if using AWS Lambda (Python)
Precautions when using TextBlob trait analysis
Authentication using tweepy-User authentication and application authentication (Python)
Send messages and images using LineNotify
Notes on using post-receive and post-merge
Precautions when using the urllib.parse.quote function
Multi-threaded Zundokokiyoshi using Event and Queue
Using Sessions and Reflections with SQLAlchemy
[Python] Be careful when using print
Clustering and visualization using Python and CytoScape
Easy and easy IoT life using Micropython!
Backtrace using DWARF information and pyelftools
Precautions when using phantomjs from python
ResourceWarning when using requests: unclosed workaround
This and that using NLTK (memo)
When using MeCab with virtualenv python
Precautions when using six with Python 2.5
[Python] File operation using if statement
[VS Code] ~ Tips when using python ~
When using regular expressions in Python
[Windows] Cause and solution of NotImplementedError when using asyncio.create_subprocess_shell () with Fast API
[Super basic] Compare Python, Java and JavaScript (variable, if statement, while statement, for statement)
Settings when using Python 3 requests and Beautiful Soup with crostini on Chromebook
Organize the flow when running Django using NGINX and gunicorn (personal memo)
Precautions and error handling when calling .NET DLL from python using pythonnet
Die when using grep's -l (list) and -v (reverse condition) options together
Settings to automatically open and close the QuickFix window when using errormaker.vim
A useful note when using Python for the first time in a while