HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Sorting seems to be slower with 2 threads instead of 1

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
sortingwiththreadsslowerseemsinstead

Problem

I'm implementing a simple merge sort to practice with Python's Threads. I've tried to split the job into two threads, that sort normally, then I can join them and merge the result. However, it takes almost twice as long with two threads than it does with the "normal" algorithm. Did I do something wrong or is it normal ? How can I improve this code to make it run faster with two threads ? What advice would you give me ? Thanks a lot.

CODE

```
# -- encoding: utf-8 --

from collections import deque
from random import randint
from time import clock
from itertools import islice
from threading import Thread

sort_dict = dict()

def merge(da, db):
res = deque()
a = da.popleft()
b = db.popleft()
while True:
if a s:
return False
except IndexError:
return True

if __name__=='__main__':
with open('results.txt', 'wb') as f:
lengths = [2*10**i for i in xrange(1, 7)]
f.writelines(['Length One_time Multi_time Ratio\r\n'])
for length in lengths:
print '----------------------------'
print 'List length : ' + str(length)
lst = [randint(0, 10**4) for i in xrange(length)]
# print 'List : ' + str(lst)
print 'ONE THREAD'
start = clock()
res = sort(lst)
one_time = clock() - start
print 'Sorting time (s) : ' + str(one_time)
print 'Sort checking : ' + str(check_sorted(res))
print 'MULTIPLE THREAD'
start = clock()
res = multi_sort(lst)
multi_time = clock() - start
print 'Sorting time (s) : ' + str(multi_time)
print 'Sort checking : ' + str(check_sorted(res))
print 'CONCLUSION'
print 'Time ratio multi/one : ' + str(multi_time/one_time)
f.writelines([str(length)+' '+
str(one_time)+' '+
str(multi_time)+' '+

Solution

This looks like a case of GIL. For actual multiprocessing you should use the multiprocessing module.


multiprocessing is a package that supports spawning processes using an API similar to the threading module. The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads. Due to this, the multiprocessing module allows the programmer to fully leverage multiple processors on a given machine. It runs on both Unix and Windows.

Context

StackExchange Code Review Q#134542, answer score: 6

Revisions (0)

No revisions yet.