3

I am new to python and I am learning threading and GIL. These are the stats oflscpu command :

 Architecture:          x86_64
 CPU op-mode(s):        32-bit, 64-bit
 Byte Order:            Little Endian
 CPU(s):                4
 On-line CPU(s) list:   0-3
 Thread(s) per core:    2
 Core(s) per socket:    2
 Socket(s):             1
 NUMA node(s):          1
 Vendor ID:             GenuineIntel
 CPU family:            6
 Model:                 69
 Stepping:              1
 CPU MHz:               1700.062
 BogoMIPS:              4789.05
 Virtualization:        VT-x
 L1d cache:             32K
 L1i cache:             32K
 L2 cache:              256K
 L3 cache:              3072K
 NUMA node0 CPU(s):     0-3

When I ran this simple python threading example, I get the following output.

import time
import threading

def counter(n):
    for i in range(0,n):
        i = i+1
    return

t1 = threading.Thread(target=counter, args = (10000000,))
t2 = threading.Thread(target=counter, args = (10000000,))

t0 = time.clock()
t1.start()
t2.start()
t1.join()
t2.join()
t3 = time.clock()

print "Total time : %s"%str(t3-t0)

bash@bash:~/Desktop$ python threads.py
Total time : 2.115326

But when I disable 3 cores and re-run the code :

bash@bash:~/Desktop$ python threads.py
Total time : 1.115442

These figures remain more or less the same. Why is this so?? Someone explain. Thanks in advance...

2rd_7
  • 462
  • 1
  • 3
  • 15

1 Answers1

2

The threading library you are using does not actually utilize multiple cores simultaneously for computation.

Try using the multiprocessing module instead for computational threading, you should see more "expected" results.

Sash Sinha
  • 18,743
  • 3
  • 23
  • 40