Search code examples
pythonperformancecomputer-science

Evaluate the performance for different parameter of a function


I want to know how a set of parameters affect the performance of a function f. I do this to store the execute time of the parameters:

ListOfParam=[param1,param2,param3]
Time=[]
for param in ListOfParam:
    start=time.time()
    f(param) 
    end=time.time()
    Time.append(end-start)

I am now worrying about whether execution of param1 will affect execution time of param2, because some param would use multiple CPUs of my computer. How to make it fair for all the param?


Solution

  • You need to use the timeit Python module. You can read the doc here, and look the examples here. Is better to use this module than implement you own timer, because timeit handle:

    • the Garbage Collector of Python to prevent that process from skewing the results by scheduling a collection run at an inopportune moment.
    • it picks the most accurate timer for your OS, time.time or time.clock.
    • it repeats the tests many times to eliminate the influence of other tasks on your machine, such as disk flushing and OS scheduling.

    Plus: IPython include the %timeit magic function for this purposes (source).