+3 votes
in Programming Languages by (3.7k points)
edited by
I have to initialize an array of length greater than a million with value 1. What is the most efficient way that will execute very fast? Will Numpy array help with the initialization?

1 Answer

0 votes
by (15.1k points)

There are several ways to initialize an array in python, but some of them take several seconds for the execution. For example, you can use one of the following ways to initialize an array with 1.

data = [1 for i in range(1000000)]

data = [1]*1000000

data = np.array([1 for i in range(1000000)])

data = np.array([1]*1000000)

All of the four approaches will initialize an array of length 1000000 with 1, but I found that data = [1]*1000000 is extremely faster than others.  Here is the code I used for testing the execution time.

import time
import numpy as np
if __name__ == '__main__':
    start_time = time.time()
    t = [1]*50000000
    print("--- %s seconds ---" % (time.time() - start_time))
    start_time = time.time()
    t = np.array([1]*50000000)
    print("--- %s seconds ---" % (time.time() - start_time))
    start_time = time.time()
    data = np.array([1 for i in range(50000000)])
    print("--- %s seconds ---" % (time.time() - start_time))

The runtimes are as follows:

--- 0.158999919891 seconds ---
--- 2.16200017929 seconds ---
--- 5.69899988174 seconds ---

You can see that t = [1]*50000000 executed in less than a second, whereas others took a few seconds for the execution.

...