Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it that np.dot is so much faster than finding the dot product using for-loops

Tags:

python

numpy

Here is the time using np.dot:

import numpy as np
import timeit

x = np.random.random(size=10**7)
a = np.ones(x.size)

%time np.dot(x, a)

Wall time: 11 ms

5001679.267011214

Here is the time using for-loops:

import numpy as np
import timeit

x = np.random.random(size=10**7)
a = np.ones(x.size)

def innfeldi(vigur1, vigur2):
    return sum([vigu1[i]*vigur2[i] for i in range(len(vigur1))])

%timeit innfeldi(x, a)

Wall time: 4.78 s

4998161.0032265792

like image 703
KristoferMarG Avatar asked Sep 12 '25 06:09

KristoferMarG


1 Answers

Because np.dot executes the actual arithmetic operations and the enclosing loop in compiled code, which is much faster than the Python interpreter.

This principle, grouping repetitive things together and cutting out the interpreter as much as possible is why we can write numerical code in high-level languages such as Python or matlab that runs at acceptable speed.

like image 188
Paul Panzer Avatar answered Sep 14 '25 19:09

Paul Panzer