• C

Problems with time.h


I wrote the following function:

void getTickCount(void) {
  clock_t start, end;
  double cpu_time_used;
  int i;
  start = clock();

  for(i = 0; i < 1000000; i++) {
    end = clock();
    if (i % 100000 == 0) {
      printf("%ld\n", end-start);
  cpu_time_used = ((end - start)) / CLOCKS_PER_SEC;
  printf("Start: %ld  End: %ld\n", start, end);
  printf("cpu_time_used: %ld\n", cpu_time_used);
  printf("cps: %i\n", (int)(CLOCKS_PER_SEC));

Which produces the following output:

Start: 1  End: 259
cpu_time_used: 1073741824
cps: 100

Now why would that cpu_time_used variable be giving me such a crazy value?!  The same value, every time, within range, but shouldn't this number be something like 2, indicating that the function took two seconds to run?  

Note:  My goal is not to measure how long it takes for the function to run, but to actually work with time in C.  Please limit your answers to fixing this code first, you can lay alternate approaches on me later...

Who is Participating?
idtConnect With a Mentor Commented:
It looks like you are using it right..
but a few observations
POSIX requires that CLOCKS_PER_SEC always equals 1000000 independantd of the actual resolution

use %f not %ld when printing doubles

cpu_time_used = ((double)(end - start)) / (double)CLOCKS_PER_SEC;
printf("cpu_time_used: %f\n", cpu_time_used);

to actualy get any decimal value

RaydotAuthor Commented:
It looks like ee deleted my comment...

Thanks for your help, what's POSIX?
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

Portable Operating System Interface (POSIX)
Originally called IEEE-IX (IEEE's version of UNIX)
The standard is heavily influenced by UNIX®

Your compiler is not POSIX compliant.  This can cause you to run into problems when trying to complile portable code to your OS.

What is your OS/compiler?
RaydotAuthor Commented:

Mac Panther / gcc

I gotta believe that's compliant, at least the gcc part.
I just compiled your code using gcc 3.2 and my output says 1000000 for CLOCKS_PER_SECOND.
I just compiled your code using icc 7.1 and my output says 1000000 for CLOCKS_PER_SECOND.

It is your include files that are non-compliant.
Follow <somepath>./include/time.h for CLOCKS_PER_SECOND.
Probably in <somepath>./include/bits/time.h for #define CLOCKS_...

There may be a remark about POSIX/ISO/IEC...

Anyway.. good luck..

RaydotAuthor Commented:
I see what you mean.  Yeah, don't know what's up with that.  I found a file that defined it as 60 for Mac, and at 1000000 for everything else.  I can't find where the libs for gcc are located.  But, I suppose that's probably fodder for a new ee question.

Thanks again.
RaydotAuthor Commented:
Sorry to bug you more, if you think I should open another question, I will.


Says that OSX's C_P_S is 100.  Good and well, but is there anything I can do about that?  Changing it in the header file won't really work, right?  Is this compiler- or machine-dependent?
RaydotAuthor Commented:
Never mind.  I ran it on Code Warrior and found the answer I was looking for, and you already said it was my compiler.
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.

All Courses

From novice to tech pro — start learning today.