Hi everyone,

I have written a program which can be performed in two ways...now,i want to compare the times taken by both approaches...can anyone help me how to get it...

Yea, here is a mediocre way :

#include <ctime>
#include <iostream>
using namespace std;

void runFunctionHere(){}

int main(){
  long start = clock(); 
      runFunctionHere();
  long end = clock();
  cout << "Time took in milli seconds : " << end - start << endl;
}

hey i tried the code..but its always showing as 0...what shall i do

#include<iostream.h>
#include<time.h>
int main()
{
clock_t start,stop;
start=clock();

::::::your function::::::

stop=clock();

cout<<(stop-start)/CLK_TCK;

return 0;
}

I hope this work.

other wise you can use this

#include<iostream.h>
#include<time.h>
#include<stddef.h>
int main()
{
time_t start,end;
start=time(NULL);

::::::Your function::::::

end=time(NULL);
cout<<difftime(end,start);

return 0;
}

The result being 0 may be due to the fact that the function runs faster than one tick of the clock() function. Most pc's from roughly 5 years ago to today support high frequency clocks which if you are running windows you can use. (if someone knows that this is in linux too and how to access it id be pleased to know about it )

Using them is the same method as above, there are 2 functions to use see link High Resolution timers Just to help out some more ill give you the layout i used for it.

class cHRTimer
{
public:
   bool Init(void); //checks to see if HRT is available
   void Start(void);
   void Stop(void);
   void Results(void);
private:
_LONG_INTEGER _start, _stop, _freq;
double m_dStart, m_dStop, m_dResult;
};

//the only function to show is results as the _LONG_INTEGER type can be awkward
void cHRTimer::Results(void)
{
  m_dStart = (double) m_start->QuadPart;
  m_dStop = (double) m_stop->QuadPart;
  m_dResult = ( (m_dStop - m_dStart) / (double) m_freq->QuadPart);
  std::cout<<"Time taken = "<<m_dResult<<" seconds"<<std::endl;
}

This gives you a timing function that can measure time down to single microSeconds (mines runs a 3.5MHZ clock so maximum resulution is .3µs but i dont think i need that kind of accuracy but at least its there :) )

Hope this helps a bit

I will advocate for Boost again... Boost has this Date-Time library which implements a microseconds clock (even a nanoseconds clock). It will run on any "normal" operating system. Of course, it will only really be as precise as your system can provide, but it will output the results in microseconds anyway.

Here is a very basic example:

#include <boost/date_time.hpp>
#include <iostream>
using namespace std;
using namespace boost::posix_time;

void runFunctionHere(){}

int main(){
  ptime start = microsec_clock::local_time(); 
      runFunctionHere();
  ptime end = microsec_clock::local_time();
  cout << "Time took in microseconds : " << (end - start).total_microseconds() << endl;
}

Here is the profiler class I use (feel free to use it):

#include <vector>
#include <fstream>
#include <ios>

#include <boost/date_time.hpp>

class exec_time_profiler {
  private:
    std::vector< boost::posix_time::ptime > mTimeTable;
    std::vector< int > mIDTable;
    
    std::ofstream mOutputStream;
    
    uint mCurrentIndex;
    uint mTotalCount;
    
  public:
    
    exec_time_profiler(const std::string& aFileName) : mTimeTable(),
                                                       mIDTable(),
                                                       mOutputStream(aFileName.c_str(),std::ios_base::out | std::ios_base::trunc),
                                                       mCurrentIndex(0),
                                                       mTotalCount(0) { };
    ~exec_time_profiler() { mOutputStream.flush(); };
    
    void markTime(int Index) {
      if(mTotalCount > 0) {
        if(mCurrentIndex < mTotalCount)
          mTimeTable[mCurrentIndex] = boost::posix_time::microsec_clock::local_time();
      } else {
        mTimeTable.push_back(boost::posix_time::microsec_clock::local_time());
        mIDTable.push_back(Index);
      };
      mCurrentIndex++;
    };
    
    void flush() {
      if(mTotalCount == 0) {
        mTotalCount = mTimeTable.size();
        for(uint i=0;i<mTotalCount-1;++i)
          mOutputStream << mIDTable[i] << " to " << mIDTable[i+1] << "\t";
      };
      mCurrentIndex = 0;
      mOutputStream << std::endl;
      for(uint i=0;i<mTotalCount-1;++i) {
        boost::posix_time::time_duration delta_t = mTimeTable[i+1] - mTimeTable[i];
        mOutputStream << delta_t.total_microseconds() << "\t";
      };
      mOutputStream.flush();
    };

};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.