pythontimemultiprocessingperformancecountertimeit

time.perf_counter() or time.perf_counter_ns() giving weird and incorrect values


I have written this simple while I was working with multi-processing.

from multiprocessing import Process, cpu_count
import time

def counter(num):
    count = 0
    while count < num:
        count += 1

def main():
    print(cpu_count())

  
    a = Process(target=counter, args=(250000000,))
    b = Process(target=counter, args=(250000000,))
    c = Process(target=counter, args=(250000000,))
    d = Process(target=counter, args=(250000000,))
    
    a.start()
    b.start()
    c.start()
    d.start()

    a.join()
    b.join()
    c.join()
    d.join()

    
    print("Finished in:", time.perf_counter(), "seconds")

if __name__ == "__main__":
    main()

Well, everything in my code was going perfectly, except the time.perf_counter() giving like 7000 seconds for a program which barely even took 2-3 seconds. I tried using the time.perf_counter_ns()but it didn't work out as I expected. Firstly, it gave me values in scientific form and when I converted nanoseconds to seconds it still gave me the same result as earlier. And rather than those, I have tried using the timeit module priniting timeit.timeit(), and it literally gave me values like 0.0143249234 for a program which took more than 2 seconds. And when I print cpu_cores instead of showing how many cores my pc has it really gave me this <bound method BaseContext.cpu_count of <multiprocessing.context.DefaultContext object at 0x000001B68DDCD1C0>>.


Solution

  • The problem you are facing is that the module gives the time in fractional seconds from a specific time, but it is not giving the elapsed time. Therefore you should mention the module once at start and once at the end and subtract the two values. And for the print(cpu_cores()) maybe you forgot to mention the brackets, therefore you are getting the position of the cores rather than the actual cores.

    I have fixed your code, and I have provided it below.

        from multiprocessing import Process, cpu_count
    import time
    
    def counter(num):
        count = 0
        while count < num:
            count += 1
    
    def main():
        print(cpu_cores())
        start_time = time.perf_counter()
        a = Process(target=counter, args=(100000,))
        a.start()
        a.join()
        end_time = time.perf_counter()
        print("Finished in:", end_time - start_time, "seconds")
    
    if __name__ == "__main__":
        main()