2

The program is a middleware between a database and application. For each database access I most calculate the time length in milliseconds. The example bellow is using TDateTime from Builder library. I must, as far as possible, only use standard c++ libraries.

AnsiString TimeInMilliseconds(TDateTime t) {
      Word Hour, Min, Sec, MSec;
      DecodeTime(t, Hour, Min, Sec, MSec);
      long ms = MSec + Sec * 1000 + Min * 1000 * 60 + Hour * 1000 * 60 * 60;
      return IntToStr(ms);
  }
  // computing times
   TDateTime SelectStart = Now();
   sql_manipulation_statement();
   TDateTime SelectEnd = Now();
Alceu Costa
  • 9,733
  • 19
  • 65
  • 83
PLS
  • 231
  • 2
  • 6
  • 14

2 Answers2

8

On both Windows and POSIX-compliant systems (Linux, OSX, etc.), you can calculate the time in 1/CLOCKS_PER_SEC (timer ticks) for a call using clock() found in <ctime>. The return value from that call will be the elapsed time since the program started running in milliseconds. Two calls to clock() can then be subtracted from each other to calculate the running time of a given block of code.

So for example:

#include <ctime>
#include <cstdio>

clock_t time_a = clock();

//...run block of code

clock_t time_b = clock();

if (time_a == ((clock_t)-1) || time_b == ((clock_t)-1))
{
    perror("Unable to calculate elapsed time");
}
else
{
    unsigned int total_time_ticks = (unsigned int)(time_b - time_a);
}

Edit: You are not going to be able to directly compare the timings from a POSIX-compliant platform to a Windows platform because on Windows clock() measures the the wall-clock time, where-as on a POSIX system, it measures elapsed CPU time. But it is a function in a standard C++ library, and for comparing performance between different blocks of code on the same platform, should fit your needs.

Jason
  • 31,834
  • 7
  • 59
  • 78
  • Note that this gives CPU time on POSIX system and, as I understand it, wall clock time on Windows. http://stackoverflow.com/questions/6114392/clocks-per-sec-not-matching-results-from-stdclock/6114447#6114447 – Fred Larson May 25 '11 at 14:16
  • In C++, it's actually in `ctime` – Etienne de Martel May 25 '11 at 14:18
  • @Fred Larson: Correct, so you are not going to be able to directly compare the timings on a windows machine directly to a POSIX machine, but on both platforms you will get a "time-based" performance indicator that should fit the OP's needs for performance monitoring that is also in a standard C++ library. – Jason May 25 '11 at 14:30
  • Its returning 0 times. I tried long instead of unsigned int but got -1 – PLS May 25 '11 at 14:30
  • 1
    1) In POSIX it isn't milliseconds, its microseconds. 2) In C and C++ standards, it isn't microseconds, its 1/CLOCKS_PER_SEC seconds. – Robᵩ May 25 '11 at 14:35
  • Re: -1. From the Linux man page "If the processor time used is not available or its value cannot be represented, the function returns the value (clock_t) -1." – Robᵩ May 25 '11 at 14:36
  • @PLS: Is the return-value from `clock()` comming back as -1? Or are you getting what would seem to be "valid" values from `clock()`, and your final calculation is coming back as -1? – Jason May 25 '11 at 14:36
  • @Jason: That may be fine, but you need to know what you're getting. Sometimes the difference is important. See the question for the answer I linked. – Fred Larson May 25 '11 at 15:07
  • @Jason I figured the output problem, thanks. Standard mesure for SQL query is its time duration. Although your solution provides a form to measure performace isn´t quite what a need. Since I can get the number of clocks per second I can transform the clock_t diff in milliseconds (I dont know yet how precise is that). Is it possible to reset the clock count? Since its starting counting when the code runs it will leak max capacity – PLS May 25 '11 at 17:48
  • Isn't time_t a better option? – PLS May 25 '11 at 18:04
  • No, that's just the number of seconds that have elapsed since January 1, 1970. Definitely not fine-grained enough. – Jason May 25 '11 at 18:09
  • @PLS: what does "leak max capacity" mean? – Jason May 25 '11 at 18:09
4

On windows you can use GetTickCount (MSDN) Which will give the number of milliseconds that have elapsed since the system was started. Using this before and after the call you get the amount of milliseconds the call took.

DWORD start = GetTickCount();
//Do your stuff
DWORD end = GetTickCount();
cout << "the call took " << (end - start) << " ms";

Edit: As Jason mentioned, Clock(); would be better because it is not related to Windows only.

RvdK
  • 19,580
  • 4
  • 64
  • 107