Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

time.perf_counter only shows units like 37724.0057971 seconds in Python

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading