-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimetest.py
44 lines (31 loc) · 972 Bytes
/
timetest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import timeit
import numpy as np
def crossterms( phase ):
cfactor = 0.
for i in range(phase.size):
for j in range(phase.size):
if i != j:
cfactor += phase.flat[i]*np.conj(phase.flat[j])
return cfactor
def squareterms( phase):
cfactor = 0.
for i in range(phase.size):
cfactor += phase.flat[i]*np.conj(phase.flat[i])
return cfactor
def sqsum(phase):
cfactor = np.abs( np.sum( phase ) )**2 - np.sum( np.abs( phase)**2 )
return cfactor
phase = np.exp( 1j * 2.*np.pi*np.random.random(20))
print phase
print
print crossterms(phase)
print squareterms(phase)
print crossterms(phase) + squareterms(phase)
print
print np.abs( np.sum(phase) ) **2
print sqsum(phase)
print
t = timeit.Timer('crossterms(phase)','from __main__ import crossterms,phase')
print round(t.timeit(100),6)
t = timeit.Timer('sqsum(phase)','from __main__ import sqsum,phase')
print round(t.timeit(100),6)