daily pastebin goal
15%
SHARE
TWEET

Untitled

a guest Jan 22nd, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import time
  2. import multiprocessing
  3.  
  4. def processSlice(id, sliceSize, delta):
  5.     sum = 0.0
  6.     for i in xrange (1 + id * sliceSize, (id + 1) * sliceSize + 1) :
  7.         x = (i - 0.5) * delta
  8.         sum += 1.0 / (1.0 + x * x)
  9.     return sum
  10.  
  11. def execute_nonParallel():
  12.    n = 100000000
  13.    delta = 1.0 / n
  14.    sliceSize = n
  15.    results = processSlice(0,sliceSize,delta)
  16.    print(results * 4.0 * delta)
  17.  
  18. def execute(processCount,pool) :
  19.     n = 100000000
  20.     delta = 1.0 / n
  21.     sliceSize = n / processCount
  22.     startTime = time.time()
  23.     results = [pool.apply_async(processSlice,args = (i,sliceSize,delta)) for i in xrange(0,processCount)]
  24.     results = [item.get() for item in results]
  25.     pi = 4.0 * sum(results) * delta
  26.     elapseTime = time.time() - startTime
  27.     print("==== Python Multiprocessing Pool pi = " + str(pi))
  28.     print("==== Python Multiprocessing Pool iteration count = "+ str(n))
  29.     print("==== Python Multiprocessing Pool elapse = " + str(elapseTime))
  30.     print("==== Python Multiprocessing Pool process count = " + str(processCount))
  31.     print("==== Python Multiprocessing Pool processor count = " + str(multiprocessing.cpu_count()))
  32.     print
  33.  
  34. if __name__ == '__main__' :
  35.    startTime = time.time()
  36.    execute_nonParallel()
  37.    print('non parallel execution time: ' + str(time.time() - startTime))
  38.    startTime = time.time()
  39.    pool = multiprocessing.Pool(processes=8)
  40.    print('pool creation time: ' + str(time.time() - startTime))
  41.    execute(1,pool)
  42.    print
  43.    execute(1,pool)
  44.    print
  45.    execute(2,pool)
  46.    print
  47.    execute(4,pool)
  48.    print
  49.    execute(8,pool)
  50.    print
  51.    pool.close()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top