Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problems with time.h

Posted on 2004-03-25
9
Medium Priority
?
1,625 Views
Last Modified: 2008-03-17
Hi,

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:

0
26
...<snip>...
207
233
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...

0
Comment
Question by:Raydot
  • 5
  • 4
9 Comments
 
LVL 3

Accepted Solution

by:
idt earned 400 total points
ID: 10679288
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

Daniel
0
 
LVL 3

Expert Comment

by:idt
ID: 10679466
further
..
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

Daniel
0
 
LVL 3

Author Comment

by:Raydot
ID: 10679472
It looks like ee deleted my comment...

Thanks for your help, what's POSIX?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 3

Expert Comment

by:idt
ID: 10679519
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?
0
 
LVL 3

Author Comment

by:Raydot
ID: 10679564
Ah.  

Mac Panther / gcc

I gotta believe that's compliant, at least the gcc part.
0
 
LVL 3

Expert Comment

by:idt
ID: 10679660
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..

Daniel
0
 
LVL 3

Author Comment

by:Raydot
ID: 10680111
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.
0
 
LVL 3

Author Comment

by:Raydot
ID: 10699432
Sorry to bug you more, if you think I should open another question, I will.

http://developer.apple.com/documentation/DeveloperTools/Conceptual/MovingProjectsToXcode/index.html?http://developer.apple.com/documentation/DeveloperTools/Conceptual/MovingProjectsToXcode/migration_differences/chapter_2_section_10.html

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?
0
 
LVL 3

Author Comment

by:Raydot
ID: 10699916
Never mind.  I ran it on Code Warrior and found the answer I was looking for, and you already said it was my compiler.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question