250 likes | 447 Views
Profiling. Chihao Li , Department of Mathematics, National Taiwan University 2011/7/7. What is Profiling?. A good program need?. Accuracy? Effeciency ? Readability? Rewritability ? Easy to debug?. When to Profile?. Time to use is “using time”!.
E N D
Profiling Chihao Li, Department of Mathematics, National Taiwan University2011/7/7
What is Profiling? Profiling
A good program need? • Accuracy? • Effeciency? • Readability? • Rewritability? • Easy to debug? Profiling
When to Profile? Profiling
Time to use is “using time”! • To evaluate the difference of executing time. • Decide which step costs the most time. • Goal: improve the efficiency. Profiling
Memory control and Debugging Profiling
prof/gprof: Execution Profiling • Generate information of the detailed executing time on each function. • Use flags: -p/-pgfor prof/gprof. • $ gcc-pg *.c • $ time ./a.out • $ gprofa.outgmon.out -bp Profiling
Assertion • Instead of using printffor debugging, we can use the macro assert: Profiling
Assertion (conti.) • gcc -o assert assert.c -lm • ./assert Profiling
Memory leak & Electric Fence • Memory leak: once you mallocthe 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. Profiling
Electric Fence Profiling
Electric Fence (conti.) • $ gcc-o efenceefence.c • $ ./efence • $ gcc -o efenceefence.c -lefence • $ ./efence • $ gcc -g -o efenceefence.c -lefence Profiling
valgrind • valgrind is another tool, you don’t need to recompile the code while using it. Profiling
vargrind (conti.) Profiling
valgrind (conti.) • $ valgrind --leak-check=yes -v ./checker Profiling
Demo Profiling
How to Profile in MATLAB? Profiling
Outline • tic & toc • clock • etime • profile Profiling
tic & toc • tic: Start a stopwatch timer. • toc: Read the stopwatch timer. • ticand tocfunction work together to measure elapsed time between the two. • Sample: Profiling
clock • 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: Profiling
etime • etime(t1, t0)returns the time in seconds that has elapsed between vectors t1andt0. • t0andt1must be the format returned by clock. • Sample: Profiling
profile • Returnssummaryoffunction calls and total time. Profiling
profile - GUI • 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. Profiling
Demo Profiling
Reference • http://blogs.mathworks.com/videos/2006/10/19/profiler-to-find-code-bottlenecks/ • 鳥哥的Linux私房菜http://linux.vbird.org/ • http://www.cprogramming.com/debugging/valgrind.html • http://catchtest.pixnet.net/blog/post/22080405 • http://nixchun.pixnet.net/blog/post/12331954 Profiling