I run this code multiple time with different SLEEP_TIME
, for example SLEEP_TIME=0
, SLEEP_TIME=1e-3
, SLEEP_TIME=10e-3
and also omitted the time.sleep line altogether from the code. For every value of SLEEP_TIME
the measured average work time changes, even though the sleep is outside the measured code. This makes zero sense to me - why would calling time.sleep change the way the process behaves even though the code absolutely does not depend on the sleep?
I tested the following code with both linux and windows and the behavior is similar (though in windows omitting the sleep altogether causes the performance to degrade significantly).
import numpy as np
import multiprocessing
import time
SLEEP_TIME = 1e-3
def do_work():
total_time = 0
time_to_run = 500
for i in range(time_to_run):
t0 = time.time()
# start work
nparr = np.ones((1000,100,30))
nparr[nparr == 0] = 1
sp = nparr.shape # to synchronize previous call
# end work
t1 = time.time()
total_time += t1 - t0
time.sleep(SLEEP_TIME) # WHY DOES THIS MATTER???? THIS IS OUTSIDE THE WORK AND OUTSIDE MEASUREMENT
print(f"avg work time: {1000 * total_time / time_to_run:.2f}ms")
if __name__ == '__main__':
p1 = multiprocessing.Process(target=do_work)
p1.start()
p2 = multiprocessing.Process(target=do_work)
p2.start()
p1.join()
p2.join()
Example results (on linux):
No sleep (commenting out time.sleep)
Output:
avg work time: 4.50ms
avg work time: 4.56ms
SLEEP_TIME = 0
Output:
avg work time: 4.46ms
avg work time: 4.52ms
SLEEP_TIME = 1e-3
Output:
avg work time: 4.76ms
avg work time: 4.82ms
SLEEP_TIME = 10e-3
Output:
avg work time: 7.05ms
avg work time: 7.07ms
What is happening here? Is the OS trying (and failing) to optimize my process? And how can I execute the work part as fast as possible regardless of the amount of previous sleep time?
ChatGPT suggested I should add to the top of the file:
import os
os.environ["OMP_NUM_THREADS"] = "1" # or whatever number you choose
While it improves the time of execution with large sleeps, the execution time still defers.
EDIT: I fixed the join strategy like some have rightly suggested. Though it's doesn't affect the problem in question it is better to write the code correctly to avoid confusion.