0
How to write timer code in python?
perform two parallel function.timer and another
2 ответов
+ 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) )
+ 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