Presentation is loading. Please wait.

Presentation is loading. Please wait.

Profiling Chihao Li, Department of Mathematics, National Taiwan University 2011/7/7.

Similar presentations


Presentation on theme: "Profiling Chihao Li, Department of Mathematics, National Taiwan University 2011/7/7."— Presentation transcript:

1 Profiling Chihao Li, Department of Mathematics, National Taiwan University 2011/7/7

2 What is profiling?Profiling2 What is Profiling?

3 A good program need?  Accuracy?  Effeciency?  Readability?  Rewritability?  Easy to debug? What is profile?Profiling

4 When to Profile?Profiling4 When to Profile?

5 Time to use is “using time”!  To evaluate the difference of executing time.  Decide which step costs the most time.  Goal: improve the efficiency. When to profile?Profiling

6 Memory control and DebuggingProfiling6 Memory control and Debugging

7 prof/gprof: Execution Profiling  Generate information of the detailed executing time on each function.  Use flags: -p/-pg for prof/gprof.  $ gcc -pg *.c  $ time./a.out  $ gprof a.out gmon.out -bp Execution ProfilingProfiling

8 Assertion  Instead of using printf for debugging, we can use the macro assert: AssertionProfiling /* assert.c */ #inlcude #include double my_sqrt(double x) { assert(x>=0.0); return sqrt(x); } int main() { printf(“sqrt +2 = %g\n”, my_sqrt(2.0)); printf(“sqrt -2 = %d\n”, my_sqrt(-2.0)); }

9 Assertion (conti.)  gcc -o assert assert.c -lm ./assert AssertionProfiling

10 Memory leak & Electric Fence  Memory leak: once you malloc the memory but forget to free it, it is hard to debug but it is very important to find out.  Eletric Fence by Bruce Perens is a useful tool. Memory leak & Electric FenceProfiling

11 Electric Fence Profiling /* efence.c */ #include int main() { char *ptr = (char*)malloc(1024); ptr[0] = 0; /*Now write beyond the block*/ ptr[1024] = 0; exit(0); }

12 Electric Fence (conti.)  $ gcc -o efence efence.c  $./efence  $ gcc -o efence efence.c -lefence  $./efence  $ gcc -g -o efence efence.c -lefence Electric FenceProfiling

13 valgrind  valgrind is another tool, you don’t need to recompile the code while using it. valgrindProfiling

14 vargrind (conti.) valgrindProfiling /* checker.c */ #include int main() { char *ptr=(char*)malloc(1024); char ch; /* Uninitialized read*/ ch = ptr[1024]; /* now write beyond the block */ ptr[1024] = 0; /* orphan block*/ ptr = 0; exit(0); }

15 valgrind (conti.)  $ valgrind --leak-check=yes -v./checker valgrindProfiling

16 DemoProfiling16 Demo

17 How to Profile in MATLAB?Profiling17 How to Profile in MATLAB?

18 Outline  tic & toc  clock  etime  profile OutlineProfiling

19 tic & toc Profiling19  tic: Start a stopwatch timer.  toc: Read the stopwatch timer.  tic and toc function work together to measure elapsed time between the two.  Sample : >> tic; % Start stopwatch. >> inv(rand(500)); % Inverse of a random matrix. >> toc % Stop and measure elapsed time.

20 clock Profiling20  Returns current date and time as a vector.  The vector is in a decimal form containing 6 elements:  [year, month, day, hour, minute, second]  Sample : >> fix(clock); % Rounds to integer display format. ans = 2011 7 7 11 22 33

21 etime Profiling21  etime(t1, t0) returns the time in seconds that has elapsed between vectors t1 and t0.  t0 and t1 must be the format returned by clock.  Sample : >> t0 = clock; % Record initial time. >> inv(rand(500)); % Inverse of a random matrix. >> t1 = clock; % Record final time. >> etime(t1, t0) % Measure elapsed time.

22 profile Profiling22  Returns summaryof function calls and total time.

23 profile - GUI etimeProfiling23  As an alternative to the profile function,  Select Desktop > Profiler to open the Profiler.  Click the “Profiler” button.  do not include “.m” in the “Run this code” field.

24 DemoProfiling24 Demo

25 Reference Profiling25  http://blogs.mathworks.com/videos/2006/ 10/19/profiler-to-find-code- bottlenecks/ http://blogs.mathworks.com/videos/2006/ 10/19/profiler-to-find-code- bottlenecks/  鳥哥的 Linux 私房菜 http://linux.vbird.org/http://linux.vbird.org/  http://www.cprogramming.com/debugging/v algrind.html http://www.cprogramming.com/debugging/v algrind.html  http://catchtest.pixnet.net/blog/post/2 2080405 http://catchtest.pixnet.net/blog/post/2 2080405  http://nixchun.pixnet.net/blog/post/123 31954 http://nixchun.pixnet.net/blog/post/123 31954


Download ppt "Profiling Chihao Li, Department of Mathematics, National Taiwan University 2011/7/7."

Similar presentations


Ads by Google