title: Python Multithreading vs Multiprocessing vs Sequential Execution
body: <p>I have the below code:</p>
  
  <pre><code>import time
  from threading import Thread
  from multiprocessing import Process 
  
  def fun1():
  
   for _ in xrange(10000000):
          print 'in fun1'
          pass
  
  def fun2():
  
   for _ in xrange(10000000):
          print 'in fun2'
          pass
  
  def fun3():
  
   for _ in xrange(10000000):
          print 'in fun3'
          pass
  
  def fun4():
  
   for _ in xrange(10000000):
          print 'in fun4'
          pass
  
  if __name__ == '__main__':
  
    #t1 = Thread(target=fun1, args=())
    t1 = Process(target=fun1, args=())
    #t2 = Thread(target=fun2, args=())
    t2 = Process(target=fun2, args=())
    #t3 = Thread(target=fun3, args=())
    t3 = Process(target=fun3, args=())
    #t4 = Thread(target=fun4, args=())
    t4 = Process(target=fun4, args=())
    t1.start()
    t2.start() 
    t3.start() 
    t4.start()
    start = time.clock()
    t1.join()
    t2.join()
    t3.join()
    t4.join()
    end = time.clock()
    print("Time Taken = ",end-start)
  
    '''
    start = time.clock()
    fun1()
    fun2()
    fun3()
    fun4()
    end = time.clock()
    print("Time Taken = ",end-start)
    '''
  </code></pre>
  
  <p>I ran the above program in three ways:</p>
  
  <ul>
  <li>First Sequential Execution ALONE(look at the commented code and comment the upper code)</li>
  <li>Second Multithreaded Execution ALONE</li>
  <li>Third Multiprocessing Execution ALONE</li>
  </ul>
  
  <p>The observations for end_time-start time are as follows:</p>
  
  <p>Overall Running times</p>
  
  <ul>
  <li>('Time Taken = ', <strong>342.5981313667716</strong>) --- Running time by <strong>threaded execution</strong></li>
  <li>('Time Taken = ', <strong>232.94691744899296</strong>) --- Running time by <strong>sequential Execution</strong></li>
  <li>('Time Taken = ', <strong>307.91093406618216</strong>) --- Running time by <strong>Multiprocessing execution</strong></li>
  </ul>
  
  <p><strong>Question :</strong> </p>
  
  <p>I see sequential execution takes least time and Multithreading takes highest time. Why? I am unable to understand and also surprised by results.Please clarify.</p>
  
  <p>Since this is a CPU intensive task and GIL is acquired, my understanding was 
  Multiprocessing would take least time while threaded execution would take highest time.Please validate my understanding. </p>