Skip to main content

No project description provided

Project description

package: TimeMyCode

The goal of this package is to time the execution of differents parts of a Python code.

1. Initiate the global timer

The package TimeMyCode depends of the built-in packages contextlib and time.

The design of the class TimeMyCode is based on the Singleton pattern. So all new instance inside the code refer finally to the same object.

To create a new Timer:

> from utils.timemycode import TimeMyCode
> 
> time_my_code = TimeMyCode()

2. Use the global timer in your code

As this package uses a context manager, we have to use it with the statement "with". To time a new part of code, we have to use a new tag.

> with time_my_code.tag("This is my new tag"):
>     ...

The same tag could be use in different parts inside your code.

3. Print the time log

At the end of the process you want to time, you could display the global time log by calling the method summary().

Note: the method summary() reinitiate the timer. After calling it, if you want to reuse the timer, you have to call again the constructor.

> print(time_my_code.summary())

Time log:
  - This is my new tag ....... 6.01s ->  1 it [6.0100 s/it]

Total time: 6.02s

Details:

  • the tag name
  • the total execution time for this tag
  • the number of iterations (number of calls of this tag)
  • the time per iteration

4. Example with a For loop

> time_my_code = TimeMyCode()
> 
> for i in range(5):
>     with time_my_code.tag('for 1'):
>         sleep(0.5)
> 
>         for i in range(15):
>             with time_my_code.tag('  for 2'):
>                 sleep(0.1)
> 
> print(time_my_code.summary())

Time log:
  - for 1 ..... 10.02s ->  5 it [2.0034 s/it]
  -   for 2 ...  7.51s -> 75 it [0.1002 s/it]

Total time: 10.02s

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

timemycode-1.0.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

timemycode-1.0.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file timemycode-1.0.0.tar.gz.

File metadata

  • Download URL: timemycode-1.0.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.8.10 Linux/5.4.0-172-generic

File hashes

Hashes for timemycode-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e7360c25fa89ae026a6589c6664fb50e5f20405ffa8f0d454de09f0c800d52f9
MD5 0cdc77e36c199fd6d3253ed53d98ac97
BLAKE2b-256 8215419984ce942d409105697b2460f1df6bc271b7db89797270378d62284350

See more details on using hashes here.

File details

Details for the file timemycode-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: timemycode-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.8.10 Linux/5.4.0-172-generic

File hashes

Hashes for timemycode-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 961b9bea73ab9862790f121c1ddfc903a7cb9cef6c6a0024311760d003488db2
MD5 4155eb87af2113a3c311895ac0a169de
BLAKE2b-256 c7ffbf8e4e71687ee7278c48a8b3cdd3c6ace64da7c0195b4b846412d71094be

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page