:cool: hi Friends,.,. !!
i want to know a perfect time consumption for a given particular program..
IN MILLISECONDs

I know the way of using following method:

start=clock();

/*...
  ...     my programming logic . . .
  ...   */

end=clock();

difference=(end-start);

But it gives time in seconds , which is not useful for me in some programs,..

Q.1
Can anyone help me to measure time in MILISECONDS ?:idea:

Q.2
Is there any TOOL which can give me perfect time consumptions ??

Please help me friends . . have a great day !

Recommended Answers

All 8 Replies

For windows, you could use getTickCount()

example:

#include <windows.h>
#include <iostream>


int main(){
    long before = GetTickCount();
    // do stuff
    std::cout << "elapsed ms: " << GetTickCount() - before;
}

For any platform, you can use Boost Date_Time library to do anything you want, and you can even get time in MICROSECONDS.

For any platform, you can use Boost Date_Time library to do anything you want, and you can even get time in MICROSECONDS.

ok boss.. i have read this and learned how to use it..
but is there any simpler tool/technique for this calculation ???
please don't mind..

Hi

simply divide clock-result by clocks per second, and don't forget the cast:

double_difference=double((end-start)) / CLOCKS_PER_SEC;

to get the correct seconds.

-- tesu

:cool: hi Friends,.,. !!
i want to know a perfect time consumption for a given particular program..
IN MILLISECONDs

I know the way of using following method:

start=clock();

/*...
  ...     my programming logic . . .
  ...   */

end=clock();

difference=(end-start);

But it gives time in seconds , which is not useful for me in some programs,..

Q.1
Can anyone help me to measure time in MILISECONDS ?:idea:

Q.2
Is there any TOOL which can give me perfect time consumptions ??

Please help me friends . . have a great day !

clock() gives time in milliseconds, not seconds. So you already got what you
need right there.

Hi firstPerson,

I am not sure of that you just stated. Below is a small program where I tested a C function which searchs for longest common substring, a question arised by peter budo about a week ago.
Actually I also had some problems with clock() taken from time.h. I did a somewhat practical calibration for the execution time shown after the end of the program was always around 2 seconds in total. This is very close to the computed value of 1.75 seconds what resulted from clock()-values (ends minus starts) divided by CLOCKS_PER_SEC, see below.

#include <iostream>
#include <string>
#include <sstream>
#include <time.h>
using namespace std;
#include "lcstring_V4.h"
int main(){ 
  // testing LCString() --> Peter Budo
  clock_t starts, ends; double dura; int i,n;
  char s1[] = "/system/images/777/medium/Debrecen_-_University.jpg?1279547675";
  char s2[] = "Debrecen_-_Protestant_Great_Church.jpg";
  char s3[strlen(s1)];
  cout << "s1: " << s1 << endl;
  cout << "s2: " << s2 << endl;
  starts = clock();
  n = 100000;
  for (int i = 0; i<n; i++) LCString (s1, s2, s3);
  ends = clock();
  dura = double((ends-starts))/ CLOCKS_PER_SEC;
  cout << "LCString (v4) callings: " << n << endl;
  cout << "Effective loopings:     " << lcstring_counter << endl;
  cout << "Worst case loopings:    " << n*strlen(s1)*strlen(s2) << endl;
  cout << "Begin (clocks):         " << starts << endl;
  cout << "End (clocks):           " << ends << endl;
  cout << "Duration (clocks):      " << ends-starts << endl;  
  cout << "Duration (sec):         " << dura << endl; 
  cout << "LCS:                    " << s3 << endl;
  cin.get();
  return 0;
/**** result
s1: /system/images/777/medium/Debrecen_-_University.jpg?1279547675
s2: Debrecen_-_Protestant_Great_Church.jpg
LCString (v4) callings: 100000
Effective loopings:     167900000
Worst case loopings:    235600000
Begin (clocks):         0
End (clocks):           1750
Duration (clocks):      1750
Duration (sec):         1.75
LCS:                    Debrecen_-_
****/
}

Can you explain what is wrong in this program when dividing the clock difference by the constant CLOCKS_PER_SEC from time.h to get seconds?

I am using gcc compiler.

Thanks,

-- tesu

.SORRY GYZ

Well well well...
I was wrong..:$
i am so sorry to all friends..
The code in my question is correctly working one . .
It gives you the perfect time consumption gyz..
I am very sorry for this thread . . :(

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.