Solved

Problems with time.h

Posted on 2004-03-25
9
1,623 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 3

Accepted Solution

by:
idt earned 100 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

630 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