Python Forum
numpy.dot() result different with classic computation for large-size arrays
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
numpy.dot() result different with classic computation for large-size arrays
#1
Basically I am testing the 1st sample code in URL https://www.geeksforgeeks.org/vectorization-in-python/

When I run the code as it is it is (array size is 100000), the result looks good - same as the output in the webpage.

But when I increase the array size to be 100000000 (100millions), I noticed the classic computation and numpy.dot result is different.

My environment:
OS: MacOS 11.6.2
Python: 3.8.9

The code:
# Dot product
import time
import numpy
import array

# 8 bytes size int
a = array.array('q')
N=100000000
for i in range(N):
        a.append(i);

b = array.array('q')
for i in range(N, 2*N):
        b.append(i)

# classic dot product of vectors implementation
tic = time.process_time()
dot = 0.0;

for i in range(len(a)):
        dot += a[i] * b[i]

toc = time.process_time()

print("dot_product = "+ str(dot));
print("Classic Computation time = " + str(1000*(toc - tic )) + "ms")

n_tic = time.process_time()
n_dot_product = numpy.dot(a, b)
n_toc = time.process_time()

print("\nn_dot_product = "+str(n_dot_product))
print("Vec Computation time = "+str(1000*(n_toc - n_tic ))+"ms")
The result is:

dot_product = 8.33333323333355e+23
Classic Computation time = 22549.694999999996ms

n_dot_product = 1659803504355747200
Vec Computation time = 131.33700000000204ms


I don't know why dot_product and n_dot_product are different. When I tried the code with N=100000, the dot_product and n_dot_product are same.
Thanks in advance.
Reply


Messages In This Thread
numpy.dot() result different with classic computation for large-size arrays - by geekgeek - Jan-25-2022, 05:35 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Parallel computation DoveV 6 1,072 Feb-07-2025, 05:01 PM
Last Post: DoveV
  Converted EXE file size is too large Rajasekaran 0 2,780 Mar-30-2023, 11:50 AM
Last Post: Rajasekaran
  problem adding two numpy arrays djf123 2 3,022 Aug-09-2022, 08:31 PM
Last Post: deanhystad
  how to join by stack multiple types in numpy arrays caro 1 1,929 Jun-20-2022, 05:02 PM
Last Post: deanhystad
  Element wise computation divon 2 2,509 Jun-01-2022, 02:36 AM
Last Post: divon
  How do I read in a Formula in Excel and convert it to do the computation in Python? JaneTan 2 3,772 Jul-07-2021, 02:06 PM
Last Post: Marbelous
  Two numpy arrays Sandra2312 1 2,425 Jan-18-2021, 06:10 PM
Last Post: paul18fr
  numpy in1d with two simple arrays claw91 3 3,477 Sep-21-2020, 12:43 PM
Last Post: scidam
  Type coercion with Numpy arrays Mark17 2 3,619 Jul-24-2020, 02:04 AM
Last Post: scidam
  filling and printing numpy arrays of str pjfarley3 4 6,376 Jun-07-2020, 09:09 PM
Last Post: pjfarley3

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020