3

If I were to use the following defined function to compute a discrete Fourier transform, how would I show that the computation scales as O(N^2) as a function of vector length.

def dft(y):
     N = len(y)
     c = np.zeros(N//2+1,complex)
     for k in range(N//2+1):
         for n in range(N):
             c[k] += y[k]*np.exp(-2j*np.pi*k*n/N)
     return c

from what I understand, if an algorithm scales as O(N^2) means that it is quadratic and the run time of the loops is proportional to the square of N. If N were doubled...then the run time would increase by N*N.

My first thought would to run a program were I transform an array of values where the length is equal to N, and then double these values (doubling N) and show that the run time difference between these two is N^2. Does this make any sense (or is there a different/better way)? If so how would I measure the run time in python?

thank you.

skyking
  • 13,817
  • 1
  • 35
  • 57
rall
  • 43
  • 3

4 Answers4

0

The runtime? You could Just make a counter at the beginning and each time something is done increase it by 1. So, inside your second for loop just increment the counter by 1, and when the program finishes print the counter. That would show the amount of calculations needed.

count = 0
def dft(y):
     N = len(y)
     c = np.zeros(N//2+1,complex)
     for k in range(N//2+1):
         for n in range(N):
             c[k] += y[k]*np.exp(-2j*np.pi*k*n/N)
             count+=1
     return c

print(count)
kyle heitman
  • 114
  • 6
  • That sounds interesting, so I use this counter function and it will essentially be telling the amount of machine instructions my program will be doing ...which I can use to demonstrate the computational scaling. Do you know of any good links to learn more about the use of this counter function in python? – rall Nov 02 '15 at 18:57
  • Um not really you just stick it in wherever you are doing something, I edited answer as an example. You could also use Skyking's aproach using an actual time. Either works for an academic assignment if they didnt exactly specify. – kyle heitman Nov 02 '15 at 19:30
  • This is long over due, but thank you for the response. It was very helpful. – rall Nov 07 '15 at 16:51
0

A little depending on what time you want to measure you could use time.clock (which I think is closest to what you want here - it measures the time shares that your program actually got to run) or datetime.datetime.now.

You just get the time before and after your calculation is done. Something like:

t0 = time.clock()
dft()
t1 = time.clock()

print("Time ellapsed: {0}".format(t1-t0))

Note that what you're looking for when doubling N is a quadrupling of the time.

skyking
  • 13,817
  • 1
  • 35
  • 57
0

The line computing the coefficients is repeated

t equation

times. Then you need to show there is a constant M and a value for N that

big o relation

as N approaches infinity. Then you've shown

result.

fireant
  • 14,080
  • 4
  • 39
  • 48
0

The timeit library is made for exactly this purpose.

https://docs.python.org/2/library/timeit.html

from timeit import timeit

for i in [10, 100, 1000, ...]:
    y = generate_array(i)
    timeit('dft(y)')
jpkotta
  • 9,237
  • 3
  • 29
  • 34