[Python-Dev] User + sys time bigger than real time, in case of no real parallelism

Marco Buttu marco.buttu at gmail.com
Sun Feb 5 01:59:04 EST 2017

I am really sorry for the OT :-( I asked elsewhere but without any 
answer :-(
I can not figure out why in this short example the user+sys time is 
bigger than real time. The example executes the task() functions twice, 
with each execution in a separate thread. The task() just increment 
10**6 times a global int:

$ cat foo.py
from threading import Thread, Lock

result = 0
lock = Lock()

def task():
     global result
     for i in range(10**6):
         result += 1

if __name__ == '__main__':
     t1, t2 = Thread(target=task), Thread(target=task)
     print('result:', result)

When I execute it (Python 3.6), I get a sys+user time bigger than the 
real time:

$ time python foo.py
result: 2000000

real   0m7.088s
user   0m6.597s
sys    0m5.043s

That is usually what I can expect in case of tasks executed in parallel 
on different CPUs. But my example should not be the case, due to the 
GIL. What am I missing? Thank you very much, and sorry again for the OT :(

Marco Buttu

INAF-Osservatorio Astronomico di Cagliari
Via della Scienza n. 5, 09047 Selargius (CA)
Phone: 070 711 80 217
Email: mbuttu at oa-cagliari.inaf.it

More information about the Python-Dev mailing list