Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 38789
  • Last Modified:

Timer function in C++

I am using alarm() to time a process.

Is there something with which i can measure by milliseconds ?

The alarm() in C++ allows only seconds and not anything lesser than that.

I did try ftime() and time(),But i need something to timeout automatically.

  • 2
1 Solution
What OS/Platform are you using?
What compiler?
karthikramAuthor Commented:
RH Linux with a g++ compiler 3.2
Why you post in the C++Builder forum then? ;)

Just kidding, however, the PTimer class sounds like a good idea to me.
There's some documentation  to it in: This class comes with pwlib-devel, you can easily get it using red carpet.

OR use POSIX timers, check Which is a good idea for portability.
//Try this!!!
LONGLONG Frequency, CurrentTime, LastTime;
    double TimeScale;

QueryPerformanceFrequency( (LARGE_INTEGER*) &Frequency);
TimeScale = (1.0/Frequency)*1000.0;

QueryPerformanceCounter( (LARGE_INTEGER*) &LastTime);//begin measure

//Add the function in here

QueryPerformanceCounter( (LARGE_INTEGER*) &CurrentTime);//end measure
AnsiString milliseconds = (CurrentTime-LastTime)*TimeScale;
//difference in milliseconds is (CurrentTime-LastTime)*TimeScale
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now