Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problems with time.h

Posted on 2004-03-25
9
Medium Priority
?
1,624 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

730 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