6.7.5.4 : What hapened if I use python list instead of numpy array for naive implementation ?
This might looks like a stupid test but let's see what will hapened.
Now, let's write the hadamardListPython.py file :
We need also to import several packages :
- sys : to make an output compatible with C++ performances output
- numpy : to deal with arrays
- astericshpc : to allocate arrays and do the performance test
1 2 3 |
import sys import numpy as np import astericshpc |
The function to evaluate performances is built the same way such as the C++ one :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
def getTimeFunctionSize(nbRepetition, nbElement): tabX = np.asarray(np.random.random(nbElement), dtype=np.float32) tabY = np.asarray(np.random.random(nbElement), dtype=np.float32) tabRes = np.zeros(nbElement, dtype=np.float32) listX = [] listY = [] listRes = [] for i in range(0,nbElement): listX.append(tabX[i]) listY.append(tabY[i]) listRes.append(tabRes[i]); timeBegin = astericshpc.rdtsc() for i in range(0, nbRepetition): for j in range(0, nbElement): listRes[j] = listX[j]*listY[j] timeEnd = astericshpc.rdtsc() elapsedTime = float(timeEnd - timeBegin)/float(nbRepetition) elapsedTimePerElement = elapsedTime/float(nbElement) print("nbElement =",nbElement,", elapsedTimePerElement =",elapsedTimePerElement,"cy/el",", elapsedTime =",elapsedTime,"cy") print(str(nbElement) + "\t" + str(elapsedTimePerElement) + "\t" + str(elapsedTime),file=sys.stderr) |
Then, we have a function to make all the points with a list of sizes :
1 2 3 |
def makeElapsedTimeValue(listSize, nbRepetition): for val in listSize: getTimeFunctionSize(nbRepetition, val) |
Finally, we call the performances tests only if this script is executed as a main file and not if it is included by an other file :
1 2 3 4 5 6 7 8 9 10 11 |
if __name__ == "__main__": listSize = [1000, 1600, 2000, 2400, 2664, 3000, 4000, 5000, 10000] makeElapsedTimeValue(listSize, 10000) |
The full hadamardListPython.py file :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
''' Auteur : Pierre Aubert Mail : pierre.aubert@lapp.in2p3.fr Licence : CeCILL-C ''' import sys import numpy as np import astericshpc def getTimeFunctionSize(nbRepetition, nbElement): tabX = np.asarray(np.random.random(nbElement), dtype=np.float32) tabY = np.asarray(np.random.random(nbElement), dtype=np.float32) tabRes = np.zeros(nbElement, dtype=np.float32) listX = [] listY = [] listRes = [] for i in range(0,nbElement): listX.append(tabX[i]) listY.append(tabY[i]) listRes.append(tabRes[i]); timeBegin = astericshpc.rdtsc() for i in range(0, nbRepetition): for j in range(0, nbElement): listRes[j] = listX[j]*listY[j] timeEnd = astericshpc.rdtsc() elapsedTime = float(timeEnd - timeBegin)/float(nbRepetition) elapsedTimePerElement = elapsedTime/float(nbElement) print("nbElement =",nbElement,", elapsedTimePerElement =",elapsedTimePerElement,"cy/el",", elapsedTime =",elapsedTime,"cy") print(str(nbElement) + "\t" + str(elapsedTimePerElement) + "\t" + str(elapsedTime),file=sys.stderr) def makeElapsedTimeValue(listSize, nbRepetition): for val in listSize: getTimeFunctionSize(nbRepetition, val) if __name__ == "__main__": listSize = [1000, 1600, 2000, 2400, 2664, 3000, 4000, 5000, 10000] makeElapsedTimeValue(listSize, 10000) |
You can download it here.