Time parts of your code easily.
It allows you to time some parts of your code in an easy way.
You can get it from pip.
pip install ttictoc
How to use it
First import the class
from ttictoc import TicToc
Now, they are several ways to use it.
Using the 'with' statement
Without creating any object you can time your code as follow.
with TicToc('name'): some code... # Prints the elapsed time
Or by creating an object you can do de same.
t = TicToc() with t: some code... # Prints the elapsed time
Calling tic toc explicitly
You can also call the tic toc explicitply as shown bellow.
t = TicToc('name') t.tic() some code... t.toc() # Prints and returns the elapsed time print(t.elapsed)
t = TicToc() t.tic() some code... t.toc() # Prints and returns the elapsed time
If you want to time multiple levels of your code, you can also do it by setting 'nested' to True.
t = TicToc(nested=True) t.tic() some code1... t.tic() some code2... t.tic() some code3... t.toc() # Prints time for code 3 t.toc() # Prints time for code 2 with code 3 t.toc() # Prints time for code 1 with code 2 and 3
Not feeling like writing 't.'?
You could do as follow.
t = TicToc() tic = t.tic toc = t.toc tic() some code... toc()
Even better, just import them.
from ttictoc import tic,toc
And use them.
tic() some code... toc()
What about nested tic tocs?
tic(nested=True) some code1... tic(nested=True) some code2... toc() # Prints the time that took some code 2 to run toc() # Prints the time that took some code 1 and 2 to run
tic(nested=True) some code1... tic() some code2... toc() # Prints the time that took some code 2 to run toc() # Prints the time that took some code 1 and 2 to run
It's the same.
set_print_toc: Sets the global behaviour of toc about printing the elapsed time. However, if you use toc(print_elapsed=True), for that specific toc the elapsed time will be printed even if
set_print_tocwas put to
set_nested: Indicates if tic toc should allow nesting.
from ttictoc import TicToc t = TicToc() t.set_nested(True) t.set_print_toc(False)
The class has 4 arguments:
name: It's the name of the object. It's not required.
method: Indicates which method should be used to get the time.
nested: Allows to use the same object several times, in different indentations to time.
print_toc: Indicate if you want to print, or not, the elapsed time when calling toc.
method argument can be either
str, or your method choice. If it's a string, the valid values are
process_time. If it's an integer, the valid values are
If python > 2
If python version >= 3.7:
In case you prefere to use other method you just do (using as example
How to "pack it"
Wiht the following command.
python setup.py sdist bdist_wheel
How to put it on pip
twine upload dist/*
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size & hash SHA256 hash help||File type||Python version||Upload date|
|ttictoc-0.4.1-py3-none-any.whl (5.0 kB) Copy SHA256 hash SHA256||Wheel||py3|
|ttictoc-0.4.1.tar.gz (4.6 kB) Copy SHA256 hash SHA256||Source||None|