Solved

Problems with time.h

Posted on 2004-03-25
9
1,607 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 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
It looks like ee deleted my comment...

Thanks for your help, what's POSIX?
0
 
LVL 3

Expert Comment

by:idt
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Author Comment

by:Raydot
Comment Utility
Ah.  

Mac Panther / gcc

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

Expert Comment

by:idt
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now