CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2010
    Posts
    54

    Timing my function

    How to time my function in linux ?

  2. #2
    Join Date
    Jul 2009
    Location
    Gothenburg
    Posts
    12

    Re: Timing my function

    Google is your friend
    http://stackoverflow.com/questions/5...-work-properly
    (I expect the code in the second answer there does pretty much what you want)
    http://stackoverflow.com/questions/5...th-c-and-linux

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Timing my function

    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  4. #4
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Timing my function

    You should really profile your code. However, if you wish to do a crude timing, and you want something that is cross-platform, then you could use boost posix microsecond clock. Here is an example header:

    Code:
    //timer.hpp
    
    #include <boost/date_time/posix_time/posix_time.hpp>        
    
    namespace timing
    {
      struct timer
      {
        timer()
          :start_time_()
        {
          reset();
        }
    
        void reset()
        {
          start_time_ = boost::posix_time::ptime(boost::posix_time::microsec_clock::local_time());
        }
    
        double elapsed() const
        {     
          double result(0);
        
          boost::posix_time::ptime now(boost::posix_time::microsec_clock::local_time());
          result = (now-start_time_).total_milliseconds()/1000.0;
        
          return result;
        }
    
       private:
         boost::posix_time::ptime start_time_;
      };
    }
    And here is a test application to run it with:
    Code:
    //main.cpp
    
    #include <boost/thread/thread.hpp>  
    #include "timer.hpp"
    
    int main()
    {
      timing::timer timer;
    
      //I've put a pause in here, but you should swap it with your
      //function call instead.
      boost::this_thread::sleep(boost::posix_time::milliseconds(10));
    
      std::cout << timer.elapsed() << std::endl;
    
      std::cout << "Press return to continue" << std::endl;
      std::cin.get();
    }
    It's never going to be as good as using a proper profiler, but I hope it helps anyway.

    By the way, assuming you have installed boost (if its not installed try installing it via the package manager), to compile the above code under linux, you'll need to type something like:

    Code:
    g++ main.cpp -O2 -lboost_thread -lboost_system

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