Post · 3 min read· pp. 1208–1210

Timer

The code in this notebook helps with measuring time.

Prerequisites

  • This notebook needs some understanding on advanced concepts in Python, notably
    • classes
    • the Python with statement
    • measuring time

Synopsis

To use the code provided in this chapter, write

text
>>> from fuzzingbook.Timer import <identifier>

and then make use of the following features.

The Timer class allows you to measure elapsed real time (in fractional seconds). Its typical usage is in conjunction with a with clause:

text
>>> with Timer() as t:
>>>     some_long_running_function()
>>> t.elapsed_time()
0.020704666967503726

Measuring Time

The class Timer allows measuring the elapsed time during some code execution.

text
import bookutils.setup
text
import time
text
def clock() -> float:
    """
    Return the number of fractional seconds elapsed since some point of reference.
    """
    return time.perf_counter()
text
from types import TracebackType
text
class Timer:
    def __init__(self) -> None:
        """Constructor"""
        self.start_time = clock()
        self.end_time = None

    def __enter__(self) -> Any:
        """Begin of `with` block"""
        self.start_time = clock()
        self.end_time = None
        return self

    def __exit__(self, exc_type: Type, exc_value: BaseException,
                 tb: TracebackType) -> None:
        """End of `with` block"""
        self.end_time = clock()

    def elapsed_time(self) -> float:
        """Return elapsed time in seconds"""
        if self.end_time is None:
            # still running
            return clock() - self.start_time
        else:
            return self.end_time - self.start_time

Here's an example:

text
def some_long_running_function() -> None:
    i = 1000000
    while i > 0:
        i -= 1
text
print("Stopping total time:")
with Timer() as t:
    some_long_running_function()
print(t.elapsed_time())
text
Stopping total time:
0.020959541958291084
text
print("Stopping time in between:")
with Timer() as t:
    for i in range(10):
        some_long_running_function()
        print(t.elapsed_time())
text
Stopping time in between:
0.01922783302143216
0.039810792019125074
0.06000966700958088
0.08149537502322346
0.10375195799861103
0.12619870802154765
0.14785908302292228
0.16952008299995214
0.19183695799438283
0.21232758299447596

That's it, folks – enjoy!

Lessons Learned

  • With the Timer class, it is very easy to measure elapsed time.

Creative Commons License The content of this project is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. The source code that is part of the content, as well as the source code used to format and display that content is licensed under the MIT License. Last change: 2023-11-11 18:25:47+01:00 • Cite • Imprint

How to Cite this Work

Andreas Zeller, Rahul Gopinath, Marcel Böhme, Gordon Fraser, and Christian Holler: "Timer". In Andreas Zeller, Rahul Gopinath, Marcel Böhme, Gordon Fraser, and Christian Holler, "The Fuzzing Book", https://www.fuzzingbook.org/html/Timer.html. Retrieved 2023-11-11 18:25:47+01:00.

text
@incollection{fuzzingbook2023:Timer,
    author = {Andreas Zeller and Rahul Gopinath and Marcel B{\"o}hme and Gordon Fraser and Christian Holler},
    booktitle = {The Fuzzing Book},
    title = {Timer},
    year = {2023},
    publisher = {CISPA Helmholtz Center for Information Security},
    howpublished = {\url{https://www.fuzzingbook.org/html/Timer.html}},
    note = {Retrieved 2023-11-11 18:25:47+01:00},
    url = {https://www.fuzzingbook.org/html/Timer.html},
    urldate = {2023-11-11 18:25:47+01:00}
}