views:

22081

answers:

6

Using only ANSI C, is there any way to measure time with milliseconds precision or more? I was browsing time.h but I only found second precision functions.

+5  A: 

The clock function returns a tick of type clock_t that is defined by a CLOCKS_PER_SEC constants. Posix requires this value to be 1000000 ticks per second (microsecond), but that is not guaranteed by ANSI-C. So, you should check CLOCKS_PER_SEC to see the actually resolution on your platform.

Judge Maygarden
That's a good point. Thank you. However, How about to create milliseconds precision timestamp from local time? I revised clock() man page and it said that the function returns the amount of processor time used since invocation of the calling process.
corto
@corto You did not include this additional requirement in your question.
Judge Maygarden
+11  A: 

There is no ANSI C function that provides better than 1 second time resolution but the POSIX function gettimeofday provides microsecond resolution. The clock function only measures the amount of time that a process has spent executing and is not accurate on many systems.

Robert Gamble
Slight caveat: gettimeofday() is not monotonic, meaning it can jump around (and even go backwards) if, for example, your machine is attempting to keep synchronisation with a network time server or some other time source.
Dipstick
A: 

The best precision you can possibly get is through the use of the x86-only "rdtsc" instruction, which can provide clock-level resolution (ne must of course take into account the cost of the rdtsc call itself, which can be measured easily on application startup).

The main catch here is measuring the number of clocks per second, which shouldn't be too hard.

Dark Shikari
You may also need to be concerned about processor affinity, because on some machines you might send the RDTSC calls to more than one processor and their RDTSC counters may not be synchronised.
Will Dean
+1  A: 
#include <time.h>
clock_t uptime = clock() / (CLOCKS_PER_SEC / 1000);
Nick
+1  A: 

I always use the clock_gettime() function, returning time from the CLOCK_MONOTONIC clock. The time returned is the amount of time, in seconds and nanoseconds, since some unspecified point in the past, such as system startup of the epoch.

#include <stdio.h>
#include <stdint.h>
#include <time.h>

int64_t timespecDiff(struct timespec *timeA_p, struct timespec *timeB_p)
{
  return ((timeA_p->tv_sec * 1000000000) + timeA_p->tv_nsec) -
           ((timeB_p->tv_sec * 1000000000) + timeB_p->tv_nsec);
}

int main(int argc, char **argv)
{
  struct timespec start, end;
  clock_gettime(CLOCK_MONOTONIC, &start);

  // Some code I am interested in measuring 

  clock_gettime(CLOCK_MONOTONIC, &end);

  uint64_t timeElapsed = timespecDiff(&end, &start);
}
clock_gettime() is not ANSI C.
20th Century Boy
Also CLOCK_MONOTONIC is not implemented on many systems (including many Linux platforms).
Dipstick
A: 

I need to source code about ticks function, would you mind help me,plz?

moni