Skip to content

Instantly share code, notes, and snippets.

@jirihnidek
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jirihnidek/8933407 to your computer and use it in GitHub Desktop.
Save jirihnidek/8933407 to your computer and use it in GitHub Desktop.
Testing of multithreading and multiprocesing in Python interpreters.
#!/usr/bin/env python
"""
Simple module for testing parallel variant of
code using Python multiprocessing
"""
from multiprocessing import Process
import time
def count_down(number):
"""
Simple function
"""
while number > 0:
number -= 1
def __main():
"""
Main function for setting and evaluating tests.
"""
count = 100000000
proc_01 = Process(target=count_down, args=(count//2,))
proc_02 = Process(target=count_down, args=(count//2,))
start = time.time()
proc_01.start()
proc_02.start()
proc_01.join()
proc_02.join()
end = time.time()
return end - start
if __name__ == '__main__':
print(__main())
#!/usr/bin/env python
"""
Simple module for testing parallel variant of
code using Python threading
"""
from threading import Thread
import time
def count_down(number):
"""
Simple function
"""
while number > 0:
number -= 1
def __main():
"""
Main function for setting and evaluating tests.
"""
count = 100000000
thread_01 = Thread(target=count_down, args=(count//2,))
thread_02 = Thread(target=count_down, args=(count//2,))
start = time.time()
thread_01.start()
thread_02.start()
thread_01.join()
thread_02.join()
end = time.time()
return end - start
if __name__ == '__main__':
print(__main())

Serial x MultiThreading

PyPy (serial)          0.14s
PyPy (parallel)        0.53s
CPython2.6 (serial)    6.80s
CPython2.6 (parallel) 12.44s
CPython2.7 (serial)    6.47s
CPython2.7 (parallel) 12.13s
CPython3.3 (serial)    8.61s
CPython3.3 (parallel) 17.37s
Jython (serial)        2.72s
Jython (parallel)      1.625

Serial x MultiProcessing

PyPy (serial)          0.14s
PyPy (parallel)        0.12s
CPython2.6 (serial)    6.80s
CPython2.6 (parallel)  3.47s
CPython2.7 (serial)    6.47s
CPython2.7 (parallel)  3.34s
CPython3.3 (serial)    8.61s
CPython3.3 (parallel)  4.65s
#!/usr/bin/env python
"""
Simple module for testing serial variant of code
"""
import time
def count_down(number):
"""
Simple function for testing.
"""
while number > 0:
number -= 1
def __main():
"""
Main function for setting and evaluating tests.
"""
count = 100000000
start = time.time()
count_down(count)
end = time.time()
return end - start
if __name__ == '__main__':
print(__main())
@jirihnidek
Copy link
Author

For more information look at this presentation: http://www.youtube.com/watch?v=Obt-vMVdM8s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment