Hi
If you want to test the power of vectorization, try the following basic example:
Concerning the 2 vectors resulting from Kronecker product, a more "visual" approach:
Paul
If you want to test the power of vectorization, try the following basic example:
import numpy as np import time def square_function(x): return x**2 n = 10_000_000; # 10 million itérations # case 1: using loops y1 = np.zeros(n, dtype = np.float); # initialization t0 = time.time(); for i in range(n): y1[i] = 0.1*i**2; t1 = time.time(); duration1 = t1 - t0; print("Duration using loops = {} seconds".format(duration1)); #case 2: using vectorization y2 = np.zeros(n, dtype = np.float); # initialization t0 = time.time(); i = np.arange(n, dtype = np.float); # vector of indexes is created y2 = 0.1*i**2; t1 = time.time(); duration2 = t1 - t0; print("Duration using Vectorization = {} seconds".format(duration2)); ## ratio calculation print("Vectorization is {} times faster than loops use!!!".format(duration1 / duration2)); print("we check there's not difference: min(y2-y1) = {} and max(y2-y1) = {}".format(np.min(y2-y1),np.max(y2-y1))); del y2; del y1; # case 3: using a function works as well y3 = np.zeros(n, dtype = np.float); # initialization t0 = time.time(); i = np.arange(n, dtype = np.float); # vector of indexes is created y3 = square_function(i); t1 = time.time(); duration3 = t1 - t0; print("Duration using Vectorization + function = {} seconds".format(duration3)); del y3;Some basics on vectorization I'm currently writing down:
Concerning the 2 vectors resulting from Kronecker product, a more "visual" approach:
Paul