Skip to content

Commit

Permalink
notes from focus session
Browse files Browse the repository at this point in the history
  • Loading branch information
teuben committed Jul 28, 2016
1 parent 8e79782 commit 400756d
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
43 changes: 43 additions & 0 deletions timing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Timing your codes:

python examples:
% ipython
run -i timing1.py
%time a=make1(1000,0)
%time a=make1(1000,1)
%time a=make1(1000,2)
%time amake1(1000,3)
%timeit make1(1000,3)

%time d=delta1(a)
%time d=delta2(a)

%timeit d=delta1(a)
%timeit d=delta2(a)


C example:
taken from NEMO http://carma.astro.umd.edu/nemo/
cd $NEMO/src/nbody/evolve/hackcode/hackcode1
make hackcode1 CC="gcc -pg"
hackcode1 nbody=1000
gprof hackcode1

# -> not so great, but this is the classic profiler

# on mac, Xcode has a good profiler
# intel compiler has a good profiler
# linux has valgrid and kcachegrind
valgrind --tool=callgrind hackcode1 nbody=1000
kcachegrind callgrind.out.16241

# note valgrind has lots of other useful option (finding memory leaks etc.)
# and also note the profiler doesn't need the -pg compile flag
make hackcode1_qp
valgrind --tool=callgrind hackcode1_qp nbody=1000
kcachegrind callgrind.out.16301

# note that the gravity routine now has twice as much CPU overhead.(25 -> 50%)

45 changes: 45 additions & 0 deletions timing/timing1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#! /usr/bin/env python
#
#
# demo: using "run -i timing1.py"

import numpy as np


def make1(n, mode=0):
if mode==0:
return np.zeros(n, dtype=np.float64) # zeros
elif mode==1:
return np.arange(float(n)) # ordinals
elif mode==2:
return np.random.uniform(0.0,1.0,int(n)) # random, uniform
elif mode==3:
return np.random.normal(0.0,1.0,int(n)) # random, normal


def make2(n, mode=0):
if mode==0:
a=[]
for i in range(n):
a.append(0)
return a
elif mode==1:
a=[]
for i in range(n):
a.append(i)
return a
elif mode==2:
return list(range(n)) # for python3 , otherwise this is a constant independant of n !!

def delta1(a):
n = len(a)
d = np.zeros(n-1)
for i in range(1,n):
d[i-1] = a[i]-a[i-1]
return d


def delta2(a):
return a[1:] - a[:-1]


0 comments on commit 400756d

Please sign in to comment.