From 400756db7c31f73b604dbb39184ad0acea88cfa5 Mon Sep 17 00:00:00 2001 From: Peter Teuben Date: Thu, 28 Jul 2016 11:45:37 -0400 Subject: [PATCH] notes from focus session --- timing/README.md | 43 +++++++++++++++++++++++++++++++++++++++++++ timing/timing1.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 timing/README.md create mode 100644 timing/timing1.py diff --git a/timing/README.md b/timing/README.md new file mode 100644 index 0000000..e9927c8 --- /dev/null +++ b/timing/README.md @@ -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%) + diff --git a/timing/timing1.py b/timing/timing1.py new file mode 100644 index 0000000..0bedfe8 --- /dev/null +++ b/timing/timing1.py @@ -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] + +