CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Sep 2009
    Location
    Pune, India
    Posts
    14

    How to do profiling in c++

    I've to measure the time taken by a c++ program to execute and also the number of clock cycles that were required to do so. Please help fast I've a deadline to meet.

    Thanks in advance

  2. #2
    Join Date
    Sep 2009
    Posts
    1

    Re: How to do profiling in c++

    Use the ctime library for clocks. The code below has not been compiled.

    Code:
    #include<iostream>
    #include<ctime>
    
    using namespace std;
    
    int main()
    {
        const float clk_strt = float( clock() );
    
        for(int i = 0; i < 100; i++ )
                cout<<i<<" ";
    
        const float clk_end = float ( clock() );
    
       cout<<"\n\nThe number of ticks is taken to execute is: " << (clk_strt - clk_end) << endl;
    
       return 0;
    
    }

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How to do profiling in c++

    If you're on Linux, you can compile using the -pg option to generate a gprof file.

  4. #4
    Join Date
    Sep 2009
    Posts
    57

    Re: How to do profiling in c++

    Quote Originally Posted by binaryHex View Post
    Use the ctime library for clocks. The code below has not been compiled.

    Code:
    #include<iostream>
    #include<ctime>
    
    using namespace std;
    
    int main()
    {
        const float clk_strt = float( clock() );
    
        for(int i = 0; i < 100; i++ )
                cout<<i<<" ";
    
        const float clk_end = float ( clock() );
    
       cout<<"\n\nThe number of ticks is taken to execute is: " << (clk_strt - clk_end) << endl;
    
       return 0;
    
    }
    To get the ticks taken to execute you must use (clk_end - clk_strt) and not the opposite.

  5. #5
    Join Date
    Sep 2009
    Location
    Pune, India
    Posts
    14

    Re: How to do profiling in c++

    thanks guys for replying..... Am working on winxp operating system and I use turbo C++. This clock() function will give me the number of cpu clock cycles or the actual time taken to execute?..... And further when I tried compiling this program the compiler was unable to include the ctime library, I got the same error in Visual C++ as well....

  6. #6
    Join Date
    Sep 2009
    Posts
    57

    Re: How to do profiling in c++

    It compiles well on code::blocks.

  7. #7
    Join Date
    Sep 2009
    Location
    Pune, India
    Posts
    14

    Re: How to do profiling in c++

    k.... I've also downloaded code::blocks......Can u tell me how to proceed further to run the program like which compiler to select etc etc..... Also if u kno a way to do all this in turbo c++ only then please tell.........

  8. #8
    Join Date
    Sep 2009
    Location
    Pune, India
    Posts
    14

    Re: How to do profiling in c++

    hey salehhamadeh pls reply dude

  9. #9
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: How to do profiling in c++

    In old compilers (like Turbo C++), you probably have to use time.h instead of ctime

    Code:
    #include <time.h>

  10. #10
    Join Date
    Sep 2009
    Location
    Pune, India
    Posts
    14

    Re: How to do profiling in c++

    thanks a lot philip..... It did work!!!!!![]

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How to do profiling in c++

    Any compiler which doesn't support the C++ standard libraries is too old to be using. Ditch it.

  12. #12
    Join Date
    Sep 2009
    Location
    Pune, India
    Posts
    14

    Re: How to do profiling in c++

    yeah u r right.... Mah college forces me 2 use it

  13. #13
    Join Date
    Sep 2009
    Posts
    57

    Re: How to do profiling in c++

    If you're forced to use it, then learning c would be better.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured