Home c++ Code Segment Time

Code Segment Time

Author

Date

Category

There is a code. How to find out his execution time in M.Sekunda or even less? I can not find anywhere (help, please. Code short, conventional time functions or simple timers do not react.

(start of timing)
// code
(End of time reference)

Thank you for your attention!


Answer 1, Authority 100%

The first answer is the easiest to fulfill, it can still be easier
Clock () To be in the library “Time.h”

int t = clock ();
code
{
}
COUT & LT; & LT; CLOCK () - T & LT; & lt; Endl;

Answer 2, Authority 100%

# include & lt; windows.h & gt;
INT MAIN ()
{
  LARGE_INTEGER T1, T2, F;
  QueryPerFormanceCounter (& amp; T1);
  // Program
  QueryPerFormanceCounter (& amp; T2);
  QueryPerFormanceFrequency (& amp; f);
  Double sec = double (T2.QuadPart - T1.QuadPart) / F.Quadpart;
  COUT & LT; & LT; "\ Ntime" & lt; & lt; sec;
}

Answer 3, Authority 50%

I used for such purposes GetTickCount . This is not milliseconds, these are tech processor. On each individual car, you can write a simple program that will measure the number of ticks per second.


Answer 4, Authority 50%

Use GetTickCount as follows

dword t = gettickcount ();
// Vasche Code
T = GetTickCount - T;
COUT & LT; & LT; T & LT; & LT; "MS" & lt; & lt; Endl;

But you need to remember that the real accuracy is 10-50 ms (depends on the accuracy of the timer).
If you need more precisely – then rdtsc


Answer 5, Authority 50%

I can advise several options:

  1. Standard C-timers, but for Pts. Quick Code will have to make it (i.e. code) cyclic so that the opening time can be measured, and then divided by the number of cycles.
  2. Use the exact OS-specific timers, such as queryperformancefrequency .
  3. Use a third-party library. As an example, I can bring the FFTW library, in which there are functions for calculating short periods of time (ticks).

I would choose the first option.

UPD.

Something like this:

# include & lt; time.h & gt;
...
const Size_t Cycles_Max = 10,000;
clock_t t0 = clock ();
For (Size_t i = 0; i & lt; cycles_max; ++ i)
{
// Measured code
}
clock_t t1 = clock ();
// Average time of performing one cycle, in sec.
Double Time_seconds = (T1-T0) * Clocks_per_sec / cycles_max;

Answer 6

Write a method that returns the current time, put it at the beginning of the program, and at the end to call again and this time takes away from the initial one – there will be a difference. Or use profiler.


Answer 7

One of the options for the extreme case. You can create the second low-priority stream, and it is looped, for example, to perform

int flag = 1; ///////////////// this is better to make a global variable
  REGISTER INT I = 0; // I write in the PERCA register, to accelerate, but it is strong
           // loads the system
  While (True) {
  SLEEP (1);
  I ++;
  if (0 == flag) break;}

This option is not the best, but working. At the same time, in the stream closing function, change the value of the Flag variable to 0. The value I will respond to milliseconds. Code of work with threads is in Google =) …

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions