My code is:
from multiprocessing import Process, cpu_count
import time
def counter(num):
count = 0
while count < num:
count += 1
def main():
a = Process(target=counter, args=(500,))
b = Process(target=counter, args=(500,))
a.start()
b.start()
a.join()
b.join()
print("finished in", time.perf_counter(), " seconds.")
if __name__ == "__main__":
main()
it takes under 1 sec yet some weird number of seconds is displayed.
I tried it with 1 billion and still got some random number like 377724.5675..,
As far as I know perf_counters shows time in seconds, but here I am confused. Can anyone explain?
>Solution :
time.perf_counter() starts from an arbitrary point in time, but counts in seconds.
If you’re looking for time elapsed, keep track of the start time, then compute a duration.
start_time = time.perf_counter()
# ... do work ...
end_time = time.perf_counter()
duration = end_time - start_time