How to write timer code in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to write timer code in python?

perform two parallel function.timer and another

25th Sep 2017, 8:27 PM
Elnaz Mahmoodi
Elnaz Mahmoodi - avatar
2 Answers
+ 2
TIMERS: I've seen CodePlayground codes using other clever ideas, this is one method I learned as "correct": import timeit do_this_many_iterations = 125000 def function_to_time(a, b, c): return sum( [a,b,c] ) # I believe you could also use globals for parameters myTimer = timeit.Timer("function_to_time(1, 2, 3)", "from __main__ import function_to_time") # Runtime in seconds, returned as a float # If you don't specify number, it defaults to 1 million iterations print( myTimer.timeit(number=do_this_many_iterations) )
25th Sep 2017, 10:07 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
MULTIPROCESSING: Notes: Python CAN run threads in parallel but isn't intended for concurrency. Google "global interpreter lock" and look into generators that "yield" control. Python is written for interleaving. On SoloLearn you must use multiprocessing.dummy (memory limit?) ... which I believe runs serially. Operating systems also interleave, which may complicate your results (CPU time vs. clock time) https://code.sololearn.com/cPyfXZGRo5sr/?ref=app
25th Sep 2017, 10:11 PM
Kirk Schafer
Kirk Schafer - avatar