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

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 :

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

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 🙂

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