Skip to main content
Make range bound more readable
Source Link
x0s
  • 1.9k
  • 20
  • 18

Here is an answer using:

  • a concise context manager to time code snippets
  • time.perf_counter() to compute time delta. It should be preferred as it is not adjustable (neither a sysadmin nor a daemon can change its value) contrary to time.time() (see doc)
import time from collections.abc import Iterator from contextlib import contextmanager @contextmanager def time_it() -> Iterator[None]: tic: float = time.perf_counter() try: yield finally: toc: float = time.perf_counter() print(f"Computation time = {1000*(toc - tic):.3f}ms") 

An example how to use it:

# Example: vector dot product computation with time_it(): A = B = range(10000001_000_000) dot = sum(a*b for a,b in zip(A,B)) # Computation time = 95.353ms 

Appendix

import time # to check adjustability assert time.get_clock_info('time').adjustable assert time.get_clock_info('perf_counter').adjustable is False 

Here is an answer using:

  • a concise context manager to time code snippets
  • time.perf_counter() to compute time delta. It should be preferred as it is not adjustable (neither a sysadmin nor a daemon can change its value) contrary to time.time() (see doc)
import time from collections.abc import Iterator from contextlib import contextmanager @contextmanager def time_it() -> Iterator[None]: tic: float = time.perf_counter() try: yield finally: toc: float = time.perf_counter() print(f"Computation time = {1000*(toc - tic):.3f}ms") 

An example how to use it:

# Example: vector dot product computation with time_it(): A = B = range(1000000) dot = sum(a*b for a,b in zip(A,B)) # Computation time = 95.353ms 

Appendix

import time # to check adjustability assert time.get_clock_info('time').adjustable assert time.get_clock_info('perf_counter').adjustable is False 

Here is an answer using:

  • a concise context manager to time code snippets
  • time.perf_counter() to compute time delta. It should be preferred as it is not adjustable (neither a sysadmin nor a daemon can change its value) contrary to time.time() (see doc)
import time from collections.abc import Iterator from contextlib import contextmanager @contextmanager def time_it() -> Iterator[None]: tic: float = time.perf_counter() try: yield finally: toc: float = time.perf_counter() print(f"Computation time = {1000*(toc - tic):.3f}ms") 

An example how to use it:

# Example: vector dot product computation with time_it(): A = B = range(1_000_000) dot = sum(a*b for a,b in zip(A,B)) # Computation time = 95.353ms 

Appendix

import time # to check adjustability assert time.get_clock_info('time').adjustable assert time.get_clock_info('perf_counter').adjustable is False 
Apply [PEP 585](https://peps.python.org/pep-0585/) recommandations for typing imports and withdraw recent python version requirement(not valid anymore)
Source Link
x0s
  • 1.9k
  • 20
  • 18

Here is an answer using:

  • a concise context manager to time code snippets
  • time.perf_counter() to compute time delta. It should be preferedpreferred as it is not adjustable (neither a sysadmin nor a daemon can change its value) contrary to time.time() (see doc)
  • python 3.10+ (because of typing but could be easily adapted to previous versions)
import time from contextlibcollections.abc import contextmanagerIterator from typingcontextlib import Iteratorcontextmanager @contextmanager def time_it() -> Iterator[None]: tic: float = time.perf_counter() try: yield finally: toc: float = time.perf_counter() print(f"Computation time = {1000*(toc - tic):.3f}ms") 

An example how to use it:

# Example: vector dot product computation with time_it(): A = B = range(1000000) dot = sum(a*b for a,b in zip(A,B)) # Computation time = 95.353ms 

Appendix

import time # to check adjustability assert time.get_clock_info('time').adjustable assert time.get_clock_info('perf_counter').adjustable is False 

Here is an answer using:

  • a concise context manager to time code snippets
  • time.perf_counter() to compute time delta. It should be prefered as it is not adjustable (neither a sysadmin nor a daemon can change its value) contrary to time.time() (see doc)
  • python 3.10+ (because of typing but could be easily adapted to previous versions)
import time from contextlib import contextmanager from typing import Iterator @contextmanager def time_it() -> Iterator[None]: tic: float = time.perf_counter() try: yield finally: toc: float = time.perf_counter() print(f"Computation time = {1000*(toc - tic):.3f}ms") 

An example how to use it:

# Example: vector dot product computation with time_it(): A = B = range(1000000) dot = sum(a*b for a,b in zip(A,B)) # Computation time = 95.353ms 

Appendix

import time # to check adjustability assert time.get_clock_info('time').adjustable assert time.get_clock_info('perf_counter').adjustable is False 

Here is an answer using:

  • a concise context manager to time code snippets
  • time.perf_counter() to compute time delta. It should be preferred as it is not adjustable (neither a sysadmin nor a daemon can change its value) contrary to time.time() (see doc)
import time from collections.abc import Iterator from contextlib import contextmanager @contextmanager def time_it() -> Iterator[None]: tic: float = time.perf_counter() try: yield finally: toc: float = time.perf_counter() print(f"Computation time = {1000*(toc - tic):.3f}ms") 

An example how to use it:

# Example: vector dot product computation with time_it(): A = B = range(1000000) dot = sum(a*b for a,b in zip(A,B)) # Computation time = 95.353ms 

Appendix

import time # to check adjustability assert time.get_clock_info('time').adjustable assert time.get_clock_info('perf_counter').adjustable is False 
Instead of storing vector coordinates in memory, `zip()` can handle generators such as `range()`
Source Link
x0s
  • 1.9k
  • 20
  • 18

Here is an answer using:

  • a concise context manager to time code snippets
  • time.perf_counter() to compute time delta. It should be prefered as it is not adjustable (neither a sysadmin nor a daemon can change its value) contrary to time.time() (see doc)
  • python 3.10+ (because of typing but could be easily adapted to previous versions)
import time from contextlib import contextmanager from typing import Iterator @contextmanager def time_it() -> Iterator[None]: tic: float = time.perf_counter() try: yield finally: toc: float = time.perf_counter() print(f"Computation time = {1000*(toc - tic):.3f}ms") 

An example how to use it:

# Example: vector dot product computation with time_it(): A = B = list(range(1000000)) dot = sum(a*b for a,b in zip(A,B)) # Computation time = 95.353ms 

Appendix

import time # to check adjustability assert time.get_clock_info('time').adjustable assert time.get_clock_info('perf_counter').adjustable is False 

Here is an answer using:

  • a concise context manager to time code snippets
  • time.perf_counter() to compute time delta. It should be prefered as it is not adjustable (neither a sysadmin nor a daemon can change its value) contrary to time.time() (see doc)
  • python 3.10+ (because of typing but could be easily adapted to previous versions)
import time from contextlib import contextmanager from typing import Iterator @contextmanager def time_it() -> Iterator[None]: tic: float = time.perf_counter() try: yield finally: toc: float = time.perf_counter() print(f"Computation time = {1000*(toc - tic):.3f}ms") 

An example how to use it:

# Example: vector dot product computation with time_it(): A = B = list(range(1000000)) dot = sum(a*b for a,b in zip(A,B)) # Computation time = 95.353ms 

Appendix

import time # to check adjustability assert time.get_clock_info('time').adjustable assert time.get_clock_info('perf_counter').adjustable is False 

Here is an answer using:

  • a concise context manager to time code snippets
  • time.perf_counter() to compute time delta. It should be prefered as it is not adjustable (neither a sysadmin nor a daemon can change its value) contrary to time.time() (see doc)
  • python 3.10+ (because of typing but could be easily adapted to previous versions)
import time from contextlib import contextmanager from typing import Iterator @contextmanager def time_it() -> Iterator[None]: tic: float = time.perf_counter() try: yield finally: toc: float = time.perf_counter() print(f"Computation time = {1000*(toc - tic):.3f}ms") 

An example how to use it:

# Example: vector dot product computation with time_it(): A = B = range(1000000) dot = sum(a*b for a,b in zip(A,B)) # Computation time = 95.353ms 

Appendix

import time # to check adjustability assert time.get_clock_info('time').adjustable assert time.get_clock_info('perf_counter').adjustable is False 
Source Link
x0s
  • 1.9k
  • 20
  • 18
Loading