Collection of Simple Python utilities.
This project is mainly used for benchmarking the kTBS.
This decorator let you time a function call.
For example, if you have some function:
def my_function():
# do some useful stuff
return something
result = my_function()
you can time it by adding the @bench
decorator:
from sutils import bench
@bench
def my_function():
# do some useful stuff
return something
time, result = my_function()
As you can see, it changes what the function call returns.
It now returns a tuple of two elements. The first one is the time taken for the function call.
The second one is what the function returns normally (i.e. what we get if we don't use @bench
).
Say you have function that you want to spawn on multiple processes. All you have to do is:
from sutils import fork
@fork(n=10)
def my_function():
# do some stuff
where n
is the number of forks you want.
This decorator use python os.fork
.