Home > Mobile >  Matrix multiplication in numpy vs normal for loop in python
Matrix multiplication in numpy vs normal for loop in python

Time:01-04

I thought of checking the time difference for a matrix multiplication using numpy matrix multiplication vs normal for loop method. I understand numpy will be faster because of vectorization, but I couldn't prove it using a simple code like below.

I am getting python normal for loop is faster than numpy in all of my test. What did I do wrong here?

My code:

# testing numpy vs normal method
def compare_np_vs_normal(datapoints):
    x = []
    for i in range(datapoints):
        x.append(math.ceil(random.random() * 10)) # random data
    
    # linear activation function
    m = math.ceil(random.random() * 10) # Random value for slope
    c = math.ceil(random.random() * 10) # Random value for intercept

    # linear activation result for all datapoints using normal method and np methods
    def normal_method():
        y = []
        for x_ in x:
            y_ = x_ * m   c
            y.append(y_)

        return y

    def np_method():
        x_ = np.c_[np.array(x),np.ones(len(x))]
        a_ = np.array([[m],[c]])
        return np.matmul(x_,a_)
        
    print("Comparing for {} datapoints".format(datapoints))
    
    print("Normal method:")
    t1 = time.perf_counter()
    y_result_normal = normal_method()
    t2 = time.perf_counter()
    print("Time taken {}".format(t2-t1))

    print("Numpy method:")
    t1 = time.perf_counter()
    y_result_np = np_method()
    t2 = time.perf_counter()
    print("Time taken {}".format(t2-t1))

    return y_result_normal, y_result_np

The result I get was

Comparing for 1000 datapoints
Normal method:
Time taken 7.759999971312936e-05
Numpy method:
Time taken 0.0007053999997879146

CodePudding user response:

You are making an overly complicated calculation in the np_method function. replace it with:

    def np_method():
        x_ = np.array(x)
        return x_ * m   c

to see the improvement

  • Related