Academic example of mergesort implementation in C (non-parallel and parallel version) with sample python ctypes bindings.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

42 lines
902 B

from __future__ import print_function
import pyms
import random
import time
len = 50000001
forkdiv = 3
print("len: {}".format(len))
foo = [x for x in range(len)]
random.shuffle(foo)
print("linear")
start = time.time()
ret = pyms.mergesort(foo)
end = time.time()
if ret < 0:
print("error: {}".format(ret));
else:
for i, e in enumerate(foo):
if i != e:
print("ERROR in sort")
break
else:
print("sort OK")
diff = end - start
print("duration: {}".format(diff))
random.shuffle(foo)
print("parallel")
start = time.time()
ret = pyms.mergesortp(foo, forkdiv)
end = time.time()
if ret < 0:
print("error: {}".format(ret));
else:
for i, e in enumerate(foo):
if i != e:
print("ERROR in sort")
break
else:
print("sort OK")
diff = end - start
print("duration: {}".format(diff))