Python array sum vs MATLAB -
i'm switching python , wanted make simple test comparing performance of simple array summation. generate random 1000x1000 array , add 1 each of values in array.
here script in python :
import time import numpy numpy.random import random def testaddone(data): """ test addone """ return data + 1 = 1000 data = random((i,i)) start = time.clock() x in xrange(1000): testaddone(data) stop = time.clock() print stop - start
and function in matlab:
function test %parameter declaration c=rand(1000); tic t = 1:1000 testaddone(c); end fprintf('structure: \n') toc end function testaddone(c) c = c + 1; end
the python takes 2.77 - 2.79 seconds, same matlab function (i'm quite impressed numpy!). have change python script use multithreading? can't in matlab since don,t have toolbox.
multi threading in python useful situations threads blocked, e.g. on getting input, not case here (see answers this question more details). however, multi processing easy in python. multiprocessing in general covered here.
a program taking similar approach example below
import time import numpy numpy.random import random multiprocessing import process def testaddone(data): return data + 1 def testaddn(data,n): # print "testaddn", n x in xrange(n): testaddone(data) if __name__ == '__main__': matrix_size = 1000 num_adds = 10000 num_processes = 4 data = random((matrix_size,matrix_size)) start = time.clock() if num_processes > 1: processes = [process(target=testaddn, args=(data,num_adds/num_processes)) in range(num_processes)] p in processes: p.start() p in processes: p.join() else: testaddn(data,num_adds) stop = time.clock() print "elapsed", stop - start
a more useful example using pool of worker processes successively add 1 different matrices below.
import time import numpy numpy.random import random multiprocessing import pool def testaddone(data): return data + 1 def testaddn(datan): data,n=datan x in xrange(n): data = testaddone(data) return data if __name__ == '__main__': num_matrices = 4 matrix_size = 1000 num_adds_per_matrix = 2500 num_processes = 4 inputs = [(random((matrix_size,matrix_size)), num_adds_per_matrix) in range(num_matrices)] #print inputs # test using, e.g., matrix_size = 2 start = time.clock() if num_processes > 1: proc_pool = pool(processes=num_processes) outputs = proc_pool.map(testaddn, inputs) else: outputs = map(testaddn, inputs) stop = time.clock() #print outputs # test using, e.g., matrix_size = 2 print "elapsed", stop - start
in case code in testaddn
result of calling testaddone
. , can uncomment print statements check useful work being done.
in both cases i've changed total number of additions 10000; fewer additions cost of starting processes becomes more significant (but can experiment parameters). , can experiment num_processes
also. on machine found compared running in same process num_processes=1
got under 2x speedup spawning 4 processes num_processes=4
.
Comments
Post a Comment