timeit#
メソッド(関数)の実行時間の計測#
from timeit import timeit
setup = 'from datetime import datetime'
statement = 'datetime.now()'
result = timeit(setup=setup, stmt=statement, number=1000) # <1>
print(f'実行時間の平均: {result / 1000}s == {result}ms')
# <1> datetime.now()の実行時間の計測(timeit で実行される)
CPU プロファイリング#
プロファイリングからコードの実行に関する情報、例えばコードの実行時間、関数の呼び出し回数などが分かる。
import random
import time
def an_expensive_function():
execution_time = random.random() / 100 # <1>実行時間はランダムに選択される
time.sleep(execution_time)
if __name__ == '__main__':
for _ in range(1000): # 1000回呼び出し
an_expensive_function()
実行例
python -m cProfile --sort cumtime cpu_profiling.py
3846 function calls (3819 primitive calls) in 18.383 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
3/1 0.000 0.000 18.383 18.383 {built-in method builtins.exec}
1 0.013 0.013 18.383 18.383 cpu_profiling.py:1(<module>)
1000 0.013 0.000 18.365 0.018 cpu_profiling.py:4(an_expensive_function)
1000 18.350 0.018 18.350 0.018 {built-in method time.sleep}
6/1 0.000 0.000 0.005 0.005 <frozen importlib._bootstrap>:1002(_find_and_load)
6/1 0.000 0.000 0.005 0.005 <frozen importlib._bootstrap>:967(_find_and_load_unlocked)
6/1 0.000 0.000 0.004 0.004 <frozen importlib._bootstrap>:659(_load_unlocked)
2/1 0.000 0.000 0.003 0.003 <frozen importlib._bootstrap_external>:784(exec_module)
10/1 0.000 0.000 0.002 0.002 <frozen importlib._bootstrap>:220(_call_with_frames_removed)
1000 0.002 0.000 0.002 0.000 {method 'random' of '_random.Random' objects}
1 0.000 0.000 0.002 0.002 random.py:1(<module>)
2 0.000 0.000 0.002 0.001 <frozen importlib._bootstrap_external>:856(get_code)
6 0.000 0.000 0.002 0.000 <frozen importlib._bootstrap>:901(_find_spec)
2 0.000 0.000 0.002 0.001 <frozen importlib._bootstrap_external>:1341(find_spec)
2 0.000 0.000 0.002 0.001 <frozen importlib._bootstrap_external>:1309(_get_spec)
8 0.000 0.000 0.001 0.000 <frozen importlib._bootstrap_external>:1438(find_spec)
12 0.000 0.000 0.001 0.000 <frozen importlib._bootstrap_external>:80(_path_stat)
12 0.001 0.000 0.001 0.000 {built-in method nt.stat}
2 0.000 0.000 0.001 0.001 <frozen importlib._bootstrap_external>:976(get_data)
2 0.001 0.000 0.001 0.000 {built-in method io.open_code}
2 0.000 0.000 0.000 0.000 {method 'read' of '_io.BufferedReader' objects}
6 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:558(module_from_spec)
2 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap_external>:1017(path_stats)
2 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap_external>:585(_compile_bytecode)
2 0.000 0.000 0.000 0.000 {built-in method marshal.loads}
2 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap_external>:99(_path_isfile)
2 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap_external>:90(_path_is_mode_type)
2 0.000 0.000 0.000 0.000 {method '__exit__' of '_io._IOBase' objects}
6 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:486(_init_module_attrs)
4 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:757(create_module)
40 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap_external>:62(_path_join)
6 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:156(__enter__)
4 0.000 0.000 0.000 0.000 {built-in method _imp.create_builtin}
1 0.000 0.000 0.000 0.000 bisect.py:1(<module>)
2 0.000 0.000 0.000 0.000 {built-in method builtins.__build_class__}
6 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:166(_get_module_lock)
4 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap_external>:301(cache_from_source)
4 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:385(cached)
40 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap_external>:64(<listcomp>)
2 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap_external>:431(_get_cached)
6 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:736(find_spec)
6 0.000 0.000 0.000 0.000 __init__.py:89(find_spec)
29 0.000 0.000 0.000 0.000 {built-in method builtins.hasattr}
1 0.000 0.000 0.000 0.000 random.py:116(__init__)
1 0.000 0.000 0.000 0.000 random.py:125(seed)
4 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:415(spec_from_loader)
6 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:58(__init__)
8 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap_external>:36(_relax_case)
34 0.000 0.000 0.000 0.000 {built-in method builtins.getattr}
2 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap_external>:1433(_get_spec)
6 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:87(acquire)
4 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap_external>:68(_path_split)
1 0.000 0.000 0.000 0.000 {function Random.seed at 0x0000019A4C3F1B80}
6 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:160(__exit__)
2 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap_external>:500(_classify_pyc)
2 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap_external>:636(spec_from_file_location)
44 0.000 0.000 0.000 0.000 {method 'join' of 'str' objects}
12 0.000 0.000 0.000 0.000 {built-in method _thread.allocate_lock}
10 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap_external>:1272(_path_importer_cache)
84 0.000 0.000 0.000 0.000 {method 'rstrip' of 'str' objects}
6 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:112(release)
4 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:765(exec_module)
46 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:231(_verbose_message)
6 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap_external>:51(_unpack_uint32)
2 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap_external>:533(_validate_timestamp_pyc)
6 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:185(cb)
16 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:878(__exit__)
1 0.000 0.000 0.000 0.000 random.py:100(Random)
6 0.000 0.000 0.000 0.000 {built-in method _imp.is_builtin}
24 0.000 0.000 0.000 0.000 {method 'rpartition' of 'str' objects}
6 0.000 0.000 0.000 0.000 {method 'format' of 'str' objects}
4 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:241(_requires_builtin_wrapper)
16 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:874(__enter__)
4 0.000 0.000 0.000 0.000 {built-in method _imp.exec_builtin}
6 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:398(parent)
6 0.000 0.000 0.000 0.000 {built-in method builtins.locals}
6 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:351(__init__)
1 0.000 0.000 0.000 0.000 {built-in method math.exp}
6 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:152(__init__)
2 0.000 0.000 0.000 0.000 {built-in method nt.getcwd}
2 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap_external>:463(_check_name_wrapper)
16 0.000 0.000 0.000 0.000 {built-in method builtins.isinstance}
28 0.000 0.000 0.000 0.000 {built-in method _imp.acquire_lock}
28 0.000 0.000 0.000 0.000 {built-in method _imp.release_lock}
4 0.000 0.000 0.000 0.000 {method 'rsplit' of 'str' objects}
2 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:35(_new_module)
2 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:811(find_spec)
12 0.000 0.000 0.000 0.000 {method '__exit__' of '_thread.lock' objects}
1 0.000 0.000 0.000 0.000 random.py:217(__init_subclass__)
6 0.000 0.000 0.000 0.000 {built-in method from_bytes}
12 0.000 0.000 0.000 0.000 {built-in method builtins.len}
12 0.000 0.000 0.000 0.000 {built-in method _thread.get_ident}
2 0.000 0.000 0.000 0.000 {method 'endswith' of 'str' objects}
12 0.000 0.000 0.000 0.000 {method 'get' of 'dict' objects}
6 0.000 0.000 0.000 0.000 {method 'pop' of 'dict' objects}
1 0.000 0.000 0.000 0.000 {built-in method math.sqrt}
2 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap_external>:946(__init__)
6 0.000 0.000 0.000 0.000 {built-in method nt.fspath}
2 0.000 0.000 0.000 0.000 {built-in method math.log}
1 0.000 0.000 0.000 0.000 random.py:770(SystemRandom)
2 0.000 0.000 0.000 0.000 {built-in method _imp.is_frozen}
6 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:406(has_location)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
6 0.000 0.000 0.000 0.000 __init__.py:96(<lambda>)
2 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap_external>:971(get_filename)
4 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:782(is_package)
2 0.000 0.000 0.000 0.000 {built-in method _imp._fix_co_filename}
2 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap_external>:781(create_module)