RE: Does clock() work?

2008-01-08 Thread Dave Korn
On 08 January 2008 17:57, Norton Allen wrote:


 Is this a known problem? Do others get this result, or do I have a
 corrupted library?

  Reproduces here.  I'll take a look into it later tonight if nobody beats me
to it.

cheers,
  DaveK
-- 
Can't think of a witty .sigline today


--
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple
Problem reports:   http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ:   http://cygwin.com/faq/



Re: Does clock() work?

2008-01-08 Thread Norton Allen

Mr Webber wrote:

CLOCKS_PER_SEC is a machine dependent macro, but not so machine dependent to
recognize that my 32-bit windows box has dual processors.  Not useful for
benchmarking, is it.
  
It's not quite clear to me why multiple processors would affect the 
interpretation of CLOCKS_PER_SEC, or why such a simple model would not 
work in a single-threaded app for basic benchmarking. I'm not talking 
about a utility to launch commercial apps (which might be multithreaded, 
etc.), just:


   * record the current time
   * do something single-threaded
   * record the current time and calculate elapsed time


clock is not the way to go. It is a crude estimation of processor time.  On
regular UNIX times(2) is the function to use -- cygwin does not seem to have
it.
  
Any other suggestions for timing resolution better than one second on 
cygwin?


-Norton


--
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple
Problem reports:   http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ:   http://cygwin.com/faq/



Re: Does clock() work?

2008-01-08 Thread Greg Chicares
On 2008-01-08 17:57Z, Norton Allen wrote:
[snip code that times this inner loop:]

   for ( i = 0; i  8; i++ ) {
 sleep(1);
 cur_time = clock();
 printf( clock() = %ld\n, cur_time );
 
 I would expect the clock() values to increase by approximately 1000 on 
 each iteration. (Yes, the sleep() seems to be working, as the lines come 
 out at about 1 Hz.)

I get similar results with the same program. According to
C99 7.23.2.1/2,
  The clock function determines the processor time used.
so I'd guess that sleep() is consuming only wall-clock time.
Here's your program with an inner loop that consumes cycles:

/tmp[0]$cat clock_test.c
#include time.h
#include unistd.h
#include stdio.h
#include limits.h

int main( int argc, char **argv ) {
  clock_t cur_time, cps = CLOCKS_PER_SEC;
  int i, j;
  volatile unsigned int v;

  printf( CLOCKS_PER_SEC = %ld\n, cps );
  for ( i = 0; i  8; i++ ) {
for ( j = 0; j  INT_MAX / 10; j++ ) {
  v++;
}
cur_time = clock();
printf( clock() = %ld\n, cur_time );
  }
  return 0;
}

/tmp[0]$gcc -o clock_test.exe clock_test.c
/tmp[0]$./clock_test
CLOCKS_PER_SEC = 1000
clock() = 437
clock() = 859
clock() = 1265
clock() = 1687
clock() = 2093
clock() = 2515
clock() = 2937
clock() = 3343

--
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple
Problem reports:   http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ:   http://cygwin.com/faq/



RE: Does clock() work?

2008-01-08 Thread Mr Webber
CLOCKS_PER_SEC is a machine dependent macro, but not so machine dependent to
recognize that my 32-bit windows box has dual processors.  Not useful for
benchmarking, is it.

clock is not the way to go. It is a crude estimation of processor time.  On
regular UNIX times(2) is the function to use -- cygwin does not seem to have
it.

 I don't know if this helps, but:

My cygwin reports
$ ./chktime
CLOCKS_PER_SEC = 1000
clock() = 31
clock() = 31
clock() = 31
clock() = 31
clock() = 31
clock() = 31
clock() = 31
clock() = 31

My 64-bit Fedora/Linux reports
# ./chktime
CLOCKS_PER_SEC = 100
clock() = 0
clock() = 0
clock() = 0
clock() = 0
clock() = 0
clock() = 0
clock() = 0
clock() = 0

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of
Norton Allen
Sent: Tuesday, January 08, 2008 12:57 PM
To: cygwin@cygwin.com
Subject: Does clock() work?

I am trying to write a benchmark application, and figured I'd use
clock() for sub-second resolution timing, but I got non-sensical results. I
check the cygwin archives, but the only mention I saw was that clock()
didn't work on Win98. Here's my test code, chktime.c:

#include time.h
#include unistd.h
#include stdio.h

int main( int argc, char **argv ) {
  clock_t cur_time, cps = CLOCKS_PER_SEC;
  int i;
 
  printf( CLOCKS_PER_SEC = %ld\n, cps );
  for ( i = 0; i  8; i++ ) {
sleep(1);
cur_time = clock();
printf( clock() = %ld\n, cur_time );
  }
  return 0;
}

and here's the output I get:

Cygwin ./chktime
CLOCKS_PER_SEC = 1000
clock() = 171
clock() = 171
clock() = 171
clock() = 171
clock() = 171
clock() = 171
clock() = 171
clock() = 171
Cygwin

I would expect the clock() values to increase by approximately 1000 on each
iteration. (Yes, the sleep() seems to be working, as the lines come out at
about 1 Hz.)

Is this a known problem? Do others get this result, or do I have a corrupted
library?

-Norton Allen


--
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple
Problem reports:   http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ:   http://cygwin.com/faq/



--
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple
Problem reports:   http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ:   http://cygwin.com/faq/



Re: Does clock() work?

2008-01-08 Thread Norton Allen

Greg Chicares wrote:

I get similar results with the same program. According to
C99 7.23.2.1/2,
  The clock function determines the processor time used.
so I'd guess that sleep() is consuming only wall-clock time.
  

Ah, good catch. Thank you.
Now, I am definitely interested in wall clock elapsed time. Is there 
anything available that will give me real time at resolution greater 
than one second?


-Norton



--
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple
Problem reports:   http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ:   http://cygwin.com/faq/



Re: Does clock() work?

2008-01-08 Thread Greg Chicares
On 2008-01-08 19:15Z, Norton Allen wrote:

 Now, I am definitely interested in wall clock elapsed time. Is there 
 anything available that will give me real time at resolution greater 
 than one second?

/tmp[0]$cat clock_test.c
#include time.h
#include unistd.h
#include stdio.h
#include sys/time.h // gettimeofday()

int main( int argc, char **argv ) {
  clock_t cur_time, cps = CLOCKS_PER_SEC;
  int i;
  struct timeval x;

  printf( CLOCKS_PER_SEC = %ld\n, cps );
  for ( i = 0; i  8; i++ ) {
sleep(1);
cur_time = clock();
printf( clock() = %ld\n, cur_time );
gettimeofday(x, 0);
printf( gettimeofday() = %ld\n, 100 * x.tv_sec + x.tv_usec );
  }
  return 0;
}

/tmp[0]$gcc -o clock_test.exe clock_test.c
/tmp[0]$./clock_test
CLOCKS_PER_SEC = 1000
clock() = 30
gettimeofday() = 210033718
clock() = 30
gettimeofday() = 211033718
clock() = 30
gettimeofday() = 212033718
clock() = 30
gettimeofday() = 213033718
clock() = 30
gettimeofday() = 214033718
clock() = 30
gettimeofday() = 215033718
clock() = 30
gettimeofday() = 216033718
clock() = 30
gettimeofday() = 217033718

--
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple
Problem reports:   http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ:   http://cygwin.com/faq/



Re: Does clock() work?

2008-01-08 Thread Norton Allen

Norton Allen wrote:

Greg Chicares wrote:

I get similar results with the same program. According to
C99 7.23.2.1/2,
  The clock function determines the processor time used.
so I'd guess that sleep() is consuming only wall-clock time.
  

Ah, good catch. Thank you.
Now, I am definitely interested in wall clock elapsed time. Is there 
anything available that will give me real time at resolution greater 
than one second?



I found gettimeofday() that seems to do the trick.
-N


--
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple
Problem reports:   http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ:   http://cygwin.com/faq/



RE: Does clock() work?

2008-01-08 Thread Mr Webber

 
Took another look and found that times(2), though not documented, is
available in Cygwin (as a macro in sys/times.h). Try it.  You should be
able to get real granular time with it, since it also returns a clock_t,
without massaging the data returned with any magic CONSTANTS that vary from
mach to mach, skewing the results.

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of
Norton Allen
Sent: Tuesday, January 08, 2008 1:40 PM
To: Mr Webber; cygwin@cygwin.com
Subject: Re: Does clock() work?

Mr Webber wrote:
 CLOCKS_PER_SEC is a machine dependent macro, but not so machine 
 dependent to recognize that my 32-bit windows box has dual processors.  
 Not useful for benchmarking, is it.
   
It's not quite clear to me why multiple processors would affect the
interpretation of CLOCKS_PER_SEC, or why such a simple model would not work
in a single-threaded app for basic benchmarking. I'm not talking about a
utility to launch commercial apps (which might be multithreaded, etc.),
just:

* record the current time
* do something single-threaded
* record the current time and calculate elapsed time

 clock is not the way to go. It is a crude estimation of processor 
 time.  On regular UNIX times(2) is the function to use -- cygwin does 
 not seem to have it.
   
Any other suggestions for timing resolution better than one second on
cygwin?

-Norton


--
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple
Problem reports:   http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ:   http://cygwin.com/faq/



RE: Does clock() work?

2008-01-08 Thread Dave Korn
On 08 January 2008 19:35, Greg Chicares wrote:

   [ TEST CASE ]

 /tmp[0]$./clock_test
 CLOCKS_PER_SEC = 1000
 clock() = 30
 gettimeofday() = 210033718
 clock() = 30
 gettimeofday() = 211033718
 clock() = 30
 gettimeofday() = 212033718
 clock() = 30
 gettimeofday() = 213033718
 clock() = 30
 gettimeofday() = 214033718
 clock() = 30
 gettimeofday() = 215033718
 clock() = 30
 gettimeofday() = 216033718
 clock() = 30
 gettimeofday() = 217033718


  clock_setres might help with that.  (Disclaimer: Unverified statement).


cheers,
  DaveK
-- 
Can't think of a witty .sigline today


--
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple
Problem reports:   http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ:   http://cygwin.com/faq/