Search code examples
pythonmultiprocessingmpi4pyelapsedtime

How to print the elapsed time in python mpi4py script?


Firstly, I know that how to record the elapsed time in the serial Python script.

import time

time_start = time.time()
...
time_end = time.time()
print("The elapsed time is", time_end-time_start, "seconds")

However, I have no clue about how to print the elapsed time if I enable multi-process by mpi4py. Could you give me examples or clarifications?


Solution

  • I'm a little late, but I've figured out how to print the elapsed time without a messy output. E.g

    import time
    time_start = time.time()
    time.sleep(3)
    time_end = time.time()
    elapsedTime = time_end - time_start
    print(f'The elapsed time is {elapsedTime} seconds')
    The elapsed time is 3.000737428665161 seconds
    

    To avoid that sloppy output, you can do this:

    import time
    time_start = time.time()
    time_end = time.time()
    elapsedTime = int(time_end - time_start)
    print(f'The elapsed time is {elapsedTime} seconds')
    The elapsed time is 3 seconds
    

    I hope this helps !