I have written a prime generator. To improve the performance i tried to use numpy for a big array.
[True for x in range(prime_max)] takes 4.5 seconds at all with prime_max = 10000000, but 0.728 to initialize
np.ones(prime_max, dtype=bool) takes 18.9 seconds at all with prime_max = 10000000 , but only 0.004 to initialize
is this because of if number_list[i] == True: ? Is there a faster way?
def prime(prime_max):
prime_list = []
number_list = [True for x in range(prime_max)]
#number_list = np.ones(prime_max, dtype=bool)
for i in range(2, prime_max):
if number_list[i] == True:
prime_list.append(i)
for j in range(2*i, prime_max, i):
number_list[j] = False
return prime_list
I know there are faster ways to generate primes, but I am learning python and first I want to understand this problem and optimize this code.