Feb-09-2018, 06:29 AM
(This post was last modified: Feb-09-2018, 06:32 AM by jiangtianyong.)
Hi ALL
I have a question when i develop a testcase to run in different python version
1: Python 2.7.10 (default, Feb 7 2017, 00:08:15) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin (here is python)
2: Python 2.7.14 (default, Jan 6 2018, 12:15:00) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin (here is python2 in my env)
3:Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin (here is my python3)
the same code run on the three different python version, i take much different time
Here is the code:
output in python 3.6.4
I have a question when i develop a testcase to run in different python version
1: Python 2.7.10 (default, Feb 7 2017, 00:08:15) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin (here is python)
2: Python 2.7.14 (default, Jan 6 2018, 12:15:00) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin (here is python2 in my env)
3:Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin (here is my python3)
the same code run on the three different python version, i take much different time

Here is the code:
#!/usr/bin/env python #coding:utf-8 ''' test GIL global interpreter lock ''' from threading import Thread import time from multiprocessing import Process from multiprocessing import Pool import multiprocessing def counter(): i = 0 for _ in range(50000000): i += 1 return True def multiprocessPool_main(): start = time.time() pool = Pool(processes=4) for x in range(10): pool.apply_async(counter, args=()) pool.close() pool.join() end = time.time() print('in pool processes take %s seconds' % (end - start)) def multiprocess_main(): start = time.time() p1 = Process(target=counter, args=()) p2 = Process(target=counter, args=()) p3 = Process(target=counter, args=()) p1.start() p2.start() p3.start() print("The number of CPU is:" + str(multiprocessing.cpu_count())) for p in multiprocessing.active_children(): print("child p.name: " + p.name + "\tp.id: " + str(p.pid)) p1.join() p2.join() p3.join() end = time.time() print('3 processes take %s seconds' % (end - start)) def main(): start_time = time.time() for tid in range(2): t = Thread(target=counter) t.start() t.join() end_time = time.time() print ("total time of single is: {}".format(end_time - start_time)) def multi_main(): thread_all = [] start_time = time.time() for tid in range(2): t = Thread(target=counter) t.start() thread_all.append(t) for i in range(2): thread_all[i].join() end_time = time.time() print ("total time of multi is: {}".format(end_time -start_time)) if __name__ == '__main__': main() multi_main() multiprocess_main() multiprocessPool_main()
Output:bogon:Documents mobike$ python3 GIL.py
total time of single is: 5.48216438293457
total time of multi is: 6.442492961883545
The number of CPU is:8
child p.name: Process-3 p.id: 8492
child p.name: Process-2 p.id: 8491
child p.name: Process-1 p.id: 8490
3 processes take 3.0569851398468018 seconds
in pool processes take 8.76835322380066 seconds
bogon:Documents mobike$ python GIL.py
total time of single is: 4.60499000549
total time of multi is: 6.05071091652
The number of CPU is:8
child p.name: Process-1 p.id: 8499
child p.name: Process-2 p.id: 8500
child p.name: Process-3 p.id: 8501
3 processes take 3.91811919212 seconds
in pool processes take 9.27075195312 seconds
bogon:Documents mobike$ python2 GIL.py
total time of single is: 13.2945930958
total time of multi is: 17.866697073
The number of CPU is:8
child p.name: Process-2 p.id: 8509
child p.name: Process-1 p.id: 8508
child p.name: Process-3 p.id: 8510
3 processes take 8.49618196487 seconds
in pool processes take 25.7352690697 seconds
output in python 3.6.4
Output:bogon:Documents mobike$ python3 GIL.py
total time of single is: 5.48216438293457
total time of multi is: 6.442492961883545
The number of CPU is:8
child p.name: Process-3 p.id: 8492
child p.name: Process-2 p.id: 8491
child p.name: Process-1 p.id: 8490
3 processes take 3.0569851398468018 seconds
in pool processes take 8.76835322380066 seconds
output in python 2.7.10Output:bogon:Documents mobike$ python GIL.py
total time of single is: 4.60499000549
total time of multi is: 6.05071091652
The number of CPU is:8
child p.name: Process-1 p.id: 8499
child p.name: Process-2 p.id: 8500
child p.name: Process-3 p.id: 8501
3 processes take 3.91811919212 seconds
in pool processes take 9.27075195312 seconds
output in the python 2.7.14Output:bogon:Documents mobike$ python2 GIL.py
total time of single is: 13.2945930958
total time of multi is: 17.866697073
The number of CPU is:8
child p.name: Process-2 p.id: 8509
child p.name: Process-1 p.id: 8508
child p.name: Process-3 p.id: 8510
3 processes take 8.49618196487 seconds
in pool processes take 25.7352690697 seconds