# [Tutor] improving speed using and recalling C functions

Gabriele Brambilla gb.gabrielebrambilla at gmail.com
Fri Apr 11 02:03:16 CEST 2014

```sure.

def mymain():

def LEstep(n):

Emin=10**6

Emax=5*(10**10)

Lemin=log10(Emin)

Lemax=log10(Emax)

stepE=(Lemax-Lemin)/n

return (stepE, n, Lemin, Lemax)

if __name__ == "__main__":

import sys

if len(sys.argv)<=1:

stepENE, nex, Lemin, Lemax = LEstep(200)

elif len(sys.argv)<=2:

stepENE, nex, Lemin, Lemax =
LEstep(int(sys.argv[1]))

else:

stepENE, nex, Lemin, Lemax =
LEstep(int(sys.argv[1]))

freq=float(sys.argv[2])

eel = list(range(nex))

eels = np.logspace(Lemin, Lemax, num=nex, endpoint=False)

indpha = list(range(npha))

indobs = list(range(nobs))

rlc = c/(2*pi*freq)

MYMAP1 = np.zeros([npha, nobs, nex], dtype=float)

MYMAP2 = np.zeros([npha, nobs, nex], dtype=float)

MYMAP3 = np.zeros([npha, nobs, nex], dtype=float)

MYMAP4 = np.zeros([npha, nobs, nex], dtype=float)

MYMAP5 = np.zeros([npha, nobs, nex], dtype=float)

count=0

omegacliston = []

alpha = '60_'

for my_line in open('datasm0_60_5s.dat'):

myinternet = []

gmlis = []

my_parts = [float(i) for i in my_line.split()]

phase = my_parts[4]

zobs = my_parts[5]

rho = my_parts[6]

gammar1 = my_parts[7]

gammar2 = my_parts[8]

gammar3 = my_parts[9]

gammar4 = my_parts[10]

gammar5 = my_parts[11]

gmlis.append(gammar1)

gmlis.append(gammar2)

gmlis.append(gammar3)

gmlis.append(gammar4)

gmlis.append(gammar5)

i = int((phase-phamin)/stepPHA)

j = int((zobs-obamin)/stepOB)

for gammar in gmlis:

omC = (1.5)*(gammar**3)*c/(rho*rlc)

gig = omC*hcut/eVtoErg

omegacliston.append(omC)

for w in eel[:]:

omega =
(10**(w*stepENE+Lemin))*eVtoErg/hcut

x = omega/omC

kap = instruments.kappa(x)

Iom = (1.732050808/c)*(e**2)*gammar*kap

P = Iom*(c/(rho*rlc))/(2*pi)

phps = P/(hcut*omega  )

www =  phps/(stepPHA*sin(zobs)*stepOB)

myinternet.append(www)

count = count + 1

oo = 0

for k in eel[:]:

MYMAP1[i, j, k] = MYMAP1[i, j, k] + myinternet[oo]

oo = oo + 1

for k in eel[:]:

MYMAP2[i, j, k] = MYMAP2[i, j, k] + myinternet[oo]

oo = oo + 1

for k in eel[:]:

MYMAP3[i, j, k] = MYMAP3[i, j, k] + myinternet[oo]

oo = oo + 1

for k in eel[:]:

MYMAP4[i, j, k] = MYMAP4[i, j, k] + myinternet[oo]

oo = oo + 1

for k in eel[:]:

MYMAP5[i, j, k] = MYMAP5[i, j, k] + myinternet[oo]

oo = oo + 1

BIGMAPS = [MYMAP1, MYMAP2, MYMAP3, MYMAP4, MYMAP5]

sigmas = [1, 3, 5, 10, 30]

fiq1 = plt.figure()

fiq2 = plt.figure()

fiq3 = plt.figure()

fiq4 = plt.figure()

fiq5 = plt.figure()

fiqqs = [fiq1, fiq2, fiq3, fiq4, fiq5]

multis = zip(sigmas, BIGMAPS, fiqqs)

for sigma, MYMAP, fiq in multis:

filename=alpha+'_'+str(sigma)+'_'+str(npha)+'_'+str(phamin)+'_'+str(phamax)+'_'+str(nobs)+'_'+str(obamin)+'_'+str(obamax)+'_'+str(nex)+'_'+str(Lemin)+'_'+str(Lemax)+'_.dat'

MYfile = open(filename, 'a')

for k in eel[:]:

for j in indobs[:]:

for i in indpha[:]:

A=MYMAP[i, j, k]

stringa = str(A) + ','

MYfile.write(stringa)

accapo = '\n'

MYfile.write(accapo)

MYfile.close()

2014-04-10 19:55 GMT-04:00 Danny Yoo <dyoo at hashcollision.org>:

>
> >    ncalls  tottime  percall  cumtime  percall filename:lineno(function)
> >         1  149.479  149.479  199.851  199.851 skymaps5.py:16(mymain)
> >  18101000   28.682    0.000   28.682    0.000 {method 'write' of 'file'
> objects}
> >
> >     33044    5.470    0.000    6.444    0.000
> interpolate.py:394(_call_linear)
> >    230000    2.272    0.000   21.279    0.000 instruments.py:10(kappa)
> >    231328    2.120    0.000    2.120    0.000
> {numpy.core.multiarray.array}
> >     33044    1.719    0.000    3.836    0.000
> interpolate.py:454(_check_bounds)
> >     66088    1.611    0.000    1.611    0.000 {method 'reduce' of
> 'numpy.ufunc'
> > objects}
> >     33044    1.146    0.000   11.623    0.000
> interpolate.py:443(_evaluate)
> >     33044    1.120    0.000    5.542    0.000
> interpolate.py:330(__init__)
> >     33044    0.659    0.000    2.329    0.000 polyint.py:82(_set_yi)
> >
> > the major time is required by mymain that is the whole program.
>
> Good!  Profiles like this allow us to pinpoint issues.
>
> Wait... ?!
>
> The profiler is saying that the majority of time is in my main, but _not_
> in auxiliary functions. That's surprising.  Am I misreading the profile?
>
> Can you show what mymain is doing?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140410/1efd6ec5/attachment-0001.html>
```