Thread: Profiler for C code

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    3

    Profiler for C code

    Hi everyone, I'm new here. I'm an italian student of informatic engineering. I'm looking for a C profiler. I try to use gprof but without success (even if I compile with -pg option). I try even Valgrind, but it's the same. Can anyone help me? Btw, I have a MacBook.
    Thanks for the help!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay, here's how to use gprof:

    1) compile: gcc -pg -fprofile-arcs -ftest-coverage
    2) now run the executable once, this will create a file "gmon.out"
    3) then gprof -b ./a.out (or the exe name).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    OK the MacBook is your problem to solve

    All joking aside, what about gprof did not work for you?

    As for Valgrind, that is a memory/leak checking tool, not a profiler...


    So if you can explain a little better what went wrong, maybe we can help you...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by MK27 View Post
    Okay, here's how to use gprof:

    1) compile: gcc -pg -fprofile-arcs -ftest-coverage
    2) now run the executable once, this will create a file "gmon.out"
    3) then gprof -b ./a.out (or the exe name).
    You beat me to it. I was going to try to get the OP to do some of the leg-work himself. You are too fast!

    Happy Tuesday...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Why not use Xcode tools?
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jeffcobb View Post
    I was going to try to get the OP to do some of the leg-work himself.
    Oh yuck -- you need to contemplate my signature Jeff. The gprof man page is one of many still used in the FIFTH CIRCLE OF HELL!!!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    3
    Quote Originally Posted by MK27 View Post
    Okay, here's how to use gprof:

    1) compile: gcc -pg -fprofile-arcs -ftest-coverage
    2) now run the executable once, this will create a file "gmon.out"
    3) then gprof -b ./a.out (or the exe name).
    But in (1) before "coverage" I insert my source code right? gcc -pg -fprofile-arcs -ftest-coverage myprog.c

    Quote Originally Posted by jeffcobb View Post
    OK the MacBook is your problem to solve
    I can say the same thing for other OS

    The code I want to profile is libsvm, if someone know. To compile this I do "make" in the dir and it compile svm-train and svm-predict.
    This what "makefile" file contains:
    Code:
    CXX ?= g++
    CFLAGS = -Wall -Wconversion -O3 -fPIC
    SHVER = 1
    
    all: svm-train svm-predict svm-scale
    
    lib: svm.o
    	$(CXX) -shared svm.o -o libsvm.so.$(SHVER)
    
    svm-predict: svm-predict.c svm.o
    	$(CXX) $(CFLAGS) svm-predict.c svm.o -o svm-predict -lm
    svm-train: svm-train.c svm.o
    	$(CXX) $(CFLAGS) svm-train.c svm.o -o svm-train -lm
    svm-scale: svm-scale.c
    	$(CXX) $(CFLAGS) svm-scale.c -o svm-scale
    svm.o: svm.cpp svm.h
    	$(CXX) $(CFLAGS) -c svm.cpp
    clean:
    	rm -f *~ svm.o svm-train svm-predict svm-scale
    I need to profile svm-train and svm-predict, where I put -pg and other things?
    I'm new in profiling and I need some help Thanks

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by guess85 View Post
    I need to profile svm-train and svm-predict, where I put -pg and other things?
    I'm new in profiling and I need some help Thanks
    Try adding it to CFLAGS.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    3
    Quote Originally Posted by MK27 View Post
    Try adding it to CFLAGS.
    Thanks, I try to do it.

  10. #10
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Yeah but I know how to profile my code
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  11. #11
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by MK27 View Post
    Oh yuck -- you need to contemplate my signature Jeff. The gprof man page is one of many still used in the FIFTH CIRCLE OF HELL!!!
    Actually I have a couple of really *really* nice volumes here that go into detail on that and all is explained well with plenty of example code.

    And I am here to help...when I first got here I would jump on any request for code and post full working solutions to everything....until I realized the number of ppl coming here waiting for the answer to just be handed to them. I have a wife, the only person I work for unconditionally for free. Others must pay and if not monetarily (nothing that I am suggesting) then through gaining personal knowledge and experience, the latter of which can be the greatest teacher of all, IMHO....this is what working in FOSS has brought to me and one of its greatest virtues: not the free code, free support, security et al: free education...


    I dunno, maybe if we had just !had that rash flea-bags posting home-work and demanding answers w/o being willing to even *try* to do the coding I might have answered differently. And the MacBook *is* his problem

    I am not picking on him; I tease all of my Apple friends.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I have altered the below to set the correct flags for profiling. Those changes are marked in blue.

    Quote Originally Posted by guess85 View Post
    Code:
    CXX ?= g++
    PROFILE = -pg
    CFLAGS = -Wall -Wconversion -O3 -fPIC $(PROFILE)
    SHVER = 1
    
    all: svm-train svm-predict svm-scale
    
    lib: svm.o
    	$(CXX) -shared svm.o -o libsvm.so.$(SHVER) $(PROFILE)
    
    svm-predict: svm-predict.c svm.o
    	$(CXX) $(CFLAGS) svm-predict.c svm.o -o svm-predict -lm
    svm-train: svm-train.c svm.o
    	$(CXX) $(CFLAGS) svm-train.c svm.o -o svm-train -lm
    svm-scale: svm-scale.c
    	$(CXX) $(CFLAGS) svm-scale.c -o svm-scale
    svm.o: svm.cpp svm.h
    	$(CXX) $(CFLAGS) -c svm.cpp
    clean:
    	rm -f *~ svm.o svm-train svm-predict svm-scale
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code profiler?
    By h3ro in forum Tech Board
    Replies: 5
    Last Post: 04-07-2008, 09:16 AM
  2. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

Tags for this Thread