i am new to programming, please tell me, why when im executing my code the timer is literally in the stratosphere?

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=(1000000000,))
    a.start()
    
    a.join()
    
    print('finished in: ', time.perf_counter(), 'seconds')

if __name__ == '__main__':
    main()

was expecting to work properly, but when i do it my timer goes like this: 692018.2843528 seconds

>Solution :

from multiprocessing import Process, cpu_count
import time

def counter(num):

    count = 0
    while count < num:
        count += 1

def main():

    # Added this
    start = time.perf_counter()

    a = Process(target=counter, args=(1000000000,))
    a.start()
    
    a.join()
    
    print('finished in: ', time.perf_counter()-start, 'seconds')

if __name__ == '__main__':
    main()

This should be what you want if I understand you correctly 🙂

Leave a Reply