-
Notifications
You must be signed in to change notification settings - Fork 0
pythonPerformance
holzkohlengrill edited this page Dec 15, 2023
·
4 revisions
This is a helpful snippet in order to measure and compare different implementations.
import timeit
import pypiscout as sc
import datetime
FACTOR = 10**7+1
class aClass:
def __init__(self):
self.a = "a"
self.b = "b"
self.c = "c"
self.d = "d"
self.e = "e"
self.f = "f"
anObj = aClass()
def function1ToMeasure():
"""
YOUR CODE
:return: None
"""
pass
def function2ToMeasure():
"""
YOUR CODE
:return: None
"""
pass
if __name__ == "__main__":
sc.header("Let's do this!")
timeStart_empty = timeit.default_timer()
for i in range(0, FACTOR):
pass
timeEnd_empty = timeit.default_timer()
sc.info("Job no load finished at: {} (duration: " "{:.12f} s)".format(datetime.datetime.now().strftime("%H:%M:%S"), (timeEnd_empty - timeStart_empty)))
# ########################
# Function 1
timeStart = timeit.default_timer()
for i in range(0, FACTOR):
function1ToMeasure()
timeEnd = timeit.default_timer()
sc.info("Job 1 finished at: {} (duration: " "{:.12f} s)".format(datetime.datetime.now().strftime("%H:%M:%S"), (timeEnd - timeStart)))
# Function 2
timeStart = timeit.default_timer()
for i in range(0, FACTOR):
function1ToMeasure()
timeEnd = timeit.default_timer()
sc.info("Job 2 at: {} (duration: " "{:.12f} s)".format(datetime.datetime.now().strftime("%H:%M:%S"), (timeEnd - timeStart)))
-
pyinstrument
(statistical profiler (polls every 1 ms the call stack)) - Pythons build-in
cProfile
- List comprehensions are generally faster than ordinary for-loops
F-strings are usually fastest (and (!) most readable). See also: https://stackoverflow.com/a/38362140/4773274
- https://wiki.python.org/moin/PythonSpeed/PerformanceTips
- Map, for, list comprehensions, disassembly
- Looping over Pandas DataFrames
Actually this is not really JIT since you compile before you run.
- HowTo using setuptools | 1
- HowTo using setuptools | 2
- Set compiler via
python.exe setup.py build_ext --inplace --compiler=msvc
(Windows example with MSVC)
top -p PID
-> However only live results (no history)
Windows performance toolkit and analyser: https://learn.microsoft.com/en-us/windows-hardware/test/wpt/
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License *.
Code (snippets) are licensed under a MIT License *.
* Unless stated otherwise