-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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%) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
|
||
|