Gilad Deutsch
Gilad Deutsch

Reputation: 405

How to get reliable results when measuring computation time of a function with python?

I wrote some function in python and want to measure its computation time for different inputs. I noticed that even when using the same input multiple times, each time the measured time will be different, I assume that's because the state of the PC might be different each time (in regards to used memory etc.) or something of the sort? Anyway, is there a way to eliminate this random factor so that the measurement becomes deterministic and reliable?

I am using the time library and the perf_counter_ns function to measure the time.

Upvotes: 0

Views: 48

Answers (1)

Ricky
Ricky

Reputation: 181

The timeit module may be what you're looking for. Here's official documentation: https://docs.python.org/3/library/timeit.html

Upvotes: 1

Related Questions