Home > Blockchain >  Timing operation with increasing list size - unexpected behaviour
Timing operation with increasing list size - unexpected behaviour

Time:11-27

Problem: How long does it take to generate a Python list of prime numbers from 1 to N? Plot a graph of time taken against N.

I used SymPy to generate the list of primes. I expected the time to increase monotonically. But why is there a dip?

import numpy as np
import matplotlib.pyplot as plt
from time import perf_counter as timer
from sympy import sieve

T = []
tic=timer()

N= np.logspace(1,8,30)
for Nup in N:
    tic = timer()
    A=list(sieve.primerange(1,Nup))
    toc = timer()
    T.append(toc-tic)
    
plt.loglog(N,T,'x-')
plt.grid()
plt.show()

warmup 1 warmup 100 warmup 10000 warmup 1000000 warmup 100000000

  • Related