GIDForums

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-May-2008, 12:31
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 385
Howard_L has a spectacular aura aboutHoward_L has a spectacular aura about

problem printing a clock_t value


Hi,
Strange, I'm having trouble with this in Fedora 6 gcc but not in mingw gcc:
CPP / C++ / C Code:
#include <stdio.h>
#include <time.h>  /* for clock_t and clock(void) */

int main(void)
{
  clock_t ct1;

  printf("CLOCKS_PER_SEC= %ld  ...press enter to stop time \n", CLOCKS_PER_SEC );
  ct1 = clock() ;
  getchar();

  ct1 = ( clock() - ct1);
  printf("ct1= %.3f \n", (ct1 / (double)CLOCKS_PER_SEC) );

  return 0;
}
I am using this line with no warnings or errors:
gcc -Wall -W -pedantic clock_t_x1.c -o clock_t_x1
I get a 2 places worth of decimal value in mingw and always this FC6 gcc:
Code:
$ ./clock_t_x1 CLOCKS_PER_SEC= 1000000 ...press enter to stop time ct1= 0.000
I don't get it... any ideas?
Howard--;
  #2  
Old 09-May-2008, 12:55
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 872
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: problem printing a clock_t value


That works for me with VC++ 6.0
Code:
CLOCKS_PER_SEC= 1000 ct1= 13.032
But your output had 1,000,000, so your number just might be VERY small -- less than the 0.000. [i.e. 0.00000123, or something smaller?]
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 09-May-2008, 13:34
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 385
Howard_L has a spectacular aura aboutHoward_L has a spectacular aura about

Re: problem printing a clock_t value


Hmm , yes that is quite a difference: 1,000 and 1,000,000.
This should at least get at the 'seconds' though , and still I get zero?:
CPP / C++ / C Code:
#include <stdio.h>
#include <time.h>  /* for clock_t and clock(void) */

int main(void)
{
  clock_t ct1;

  printf("CLOCKS_PER_SEC= %ld ...wait for it! (press enter) ",
                   CLOCKS_PER_SEC );
  ct1 = clock() ;
  
  getchar();
  printf("ct1= %ld    ", (ct1 / CLOCKS_PER_SEC) );
  printf("clock()= %ld   ", clock() );
  printf("clock()= %ld \n", clock()/CLOCKS_PER_SEC  );
  return 0;
} 

/* 

In fc6 I get this:
CLOCKS_PER_SEC= 1000000 ...wait for it! (press enter) 
ct1= 0    clock()= 0   clock()= 0

In mingw I get this:
clock_t_x2.exe
CLOCKS_PER_SEC= 1000 ...wait for it! (press enter)
ct1= 0    clock()= 7630    clock()= 7
*/
Is my fc6 gcc broken? gcc version 4.1.1 20061011 (Red Hat 4.1.1-30)
  #4  
Old 09-May-2008, 14:12
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 872
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: problem printing a clock_t value


From what I can find through google, that gcc version is NOT broken, but there has been a change to the CLOCKS_PER_SEC value for ansi compliance. (see comments in time.h)

I'm not sure what option may need to be toggled for the old value.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #5  
Old 09-May-2008, 14:54
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 385
Howard_L has a spectacular aura aboutHoward_L has a spectacular aura about

Re: problem printing a clock_t value


Ahhh! Thanks for the help but I think I found a different answer! (for now):
CPP / C++ / C Code:
#include<time.h>
#include<stdio.h>

int main(void)
{
  clock_t ct1, ct2;
  int i;

  ct1 = clock();
  /* use loop instead of a getchar() to let time pass */
  for(i= 1; i > 0; i++)
  ;
  ct2 = clock();

  printf("maximum values of int on this machine are: %d and %d  \n ", i, i-1);
  printf("    ...and it took %ld ticks to count that high! \n", ct2 - ct1 );
  printf("Which is %.4f seconds.\n", ((ct2 - ct1) / (double)CLOCKS_PER_SEC) );

  return 0;
}
/* based on acm.uiuc.edu example which finally got a value to print in fc6! 
   so it would appear that a linux process sleeps or something at getchar(); 
   and on a MS machine it stays awake.   So my original trials were not 
   taking enough process time to show any elapsed time! ha!
*/ 
Code:
$ time ./clock_t_x2 maximum values of int on this machine are: -2147483648 and 2147483647 ...and it took 30290000 ticks to count that high! Which is 30.2900 seconds. real 0m30.366s user 0m30.294s sys 0m0.004s ...now isn't that fun...
Curiously, I was writing that program to burn some process time to observe with the linux 'top' and 'ps' commands!
Howard(:-);
 

Recent GIDBlogNon-US citizens serving in the military by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem printing pictures stupidcomputer Computer Software Forum - Windows 1 15-Oct-2007 11:22
Runtime Problem involving "printf" in C Program supamakia C Programming Language 2 09-Oct-2005 10:09
Printing Problem! Salchester CPP / C++ Forum 3 07-Sep-2005 17:13
problem printing output iamsodumb CPP / C++ Forum 4 10-Mar-2005 13:19
Printing problem Frandy MS Visual C++ / MFC Forum 1 16-Feb-2005 13:47

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:01.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.