"Bill Reid" <ho********@happyhealthy.netwrites:
Keith Thompson <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
>"Bill Reid" <ho********@happyhealthy.netwrites:
[...]
Ah yes, the non-sequitur vulgar life strategy tactic. I NEVER
asked for any help here, just pointed out your and Keith Thompson's
errors of understanding the "C" standard and computer science
after the 1970s...
[...]
I don't believe I've misunderstood the C standard, but I never quite
understood what the program you posted is doing, partly because it
used some non-standard function called "_sleep".
Here's what the standard says about the clock() function
(C99 7.23.2.1):
[...]
>Perhaps you can shed some light on this.
[...]
But <big sigh, waiting for this to snipped out again>, AS I
SAID IN THE POST YOU ARE RESPONDING TO, you
are missing/misinterpreting a key word in the standard:
"The clock function returns the implementation's best approximation..."
"APPROXIMATION"! SEE IT?!??!!!
"APPROXIMATION"!!! SEE IT!!????!!!?!!
"APPROXIMATION"!!!!!!! SEE IT??!???!!!??!??!!!
Your so-called "requirement" is only an "APPROXIMATION"
by the "IMPLEMENTATION".
[...]
MY OS apparently just returns something that is effectively
the same as "wall clock" time as ITS "approximation" of "processor
time".
[...]
Then you'll understand perfectly, without even being told, what
the pesky "_sleep" function is doing, and you'll be so "enlightened"
that you'll suddenly "grok" why that "confusing" six-line program
I wrote behaves the way it does (and why there's a good chance
it would behave that way on a LOT of systems).
[...]
Stop shouting, and stop treating us like idiots just because we may
have missed (or disagreed with) some point you made. I'm trying to
take part in a technical discussion here. You're welcome to join me
in that endeavor.
Yes, the value returned by the clock() function is an *approximation*.
I'm very well aware of that; I don't expect it to count every CPU
cycle unless the underlying system makes it reasonably easy to do
that.
Unless I've missed something, you still haven't explained what
_sleep() does; you've just insulted me for not already knowing. I
could guess that it behaves similarly to the POSIX sleep() function,
i.e., that it suspends execution of the current program (process,
whatever) for a specified time interval, allowing other programs
(processes, whatever) to use the CPU until the specified interval
expires. But since it's a non-standard function, I didn't see any
point in making even an educated guess when you could easily tell us
what it does.
The point is that, based on your description, buried somewhere within
the insults, your system's clock() function appears to be broken.
Yes, the standard only requires an "approximation", but did you miss
the context: "best approximation"?
If your system's clock() function indicates that your program used 4
seconds of processor time while it was sleeping for 4 seconds and not
using the processor, then I can hardly imagine that that's "the
implementation's best approximation to the processor time used by the
program". A sin() function that always returns 0.0 would be a
similarly bad approximation; you might be able to argue that the
standard allows it, but it would unquestionably be broken.
So the whole point of this discussion is apparently that your
implementation has a broken, or at least non-standard, clock()
function.
You claim that there's a good chance that your program would behave
the same way on a lot of systems. I believe you're mistaken. For one
thing, most systems aren't likely to have a function called '_sleep'.
Here's my version of your program. It depends on the POSIX sleep()
function, but is otherwise standard C. Note that the standard says
only that clock_t is an arithmetic type, so I've allowed for all the
possibilities (floating, unsigned, and signed). I've also used the
standard CLOCKS_PER_SEC macro rather than CLK_TCK. And I've examined
the result of clock() again after performing some CPU-intensive
calculatations. A decent optimizer could eliminate the calculations,
but I compiled the program without asking for optimization. If
necessary, I could have declared 'result' as volatile.
I observed, as the program ran, that each sleep(N) call took
approximately N seconds (I didn't time it precisely).
================================================== ======================
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <math.h>
static void show_clock(clock_t c)
{
if ((clock_t)1 / 2 (clock_t)0) {
/* clock_t is floating-point */
printf("%f", (double)c);
}
else if ((clock_t)-1 (clock_t)0) {
/* clock_t is unsigned */
printf("%luU", (unsigned long)c);
}
else {
/* clock_t is signed */
printf("%ld", (long)c);
}
}
static void do_stuff(void)
{
#define ITERATIONS 10000000
long i;
double result;
for (i = 0; i < ITERATIONS; i ++) {
result = sin((double)i/ITERATIONS);
}
}
int main(void)
{
int inc;
clock_t start, end;
printf("CLOCKS_PER_SEC = ");
show_clock(CLOCKS_PER_SEC);
putchar('\n');
for (inc = 1; inc < 5; inc++) {
printf("Sleeping for %d seconds\n", inc);
start = clock();
sleep(inc);
end = clock();
printf("start = ");
show_clock(start);
printf(", end = ");
show_clock(end);
putchar('\n');
printf("Slept for %f seconds of processor time\n",
(((double)end-start)/CLOCKS_PER_SEC));
}
do_stuff();
printf("After computations, clock() returns ");
show_clock(clock());
putchar('\n');
return 0;
}
================================================== ======================
And here's the output I got on one system:
================================================== ======================
CLOCKS_PER_SEC = 1000000
Sleeping for 1 seconds
start = 0, end = 0
Slept for 0.000000 seconds of processor time
Sleeping for 2 seconds
start = 0, end = 0
Slept for 0.000000 seconds of processor time
Sleeping for 3 seconds
start = 0, end = 0
Slept for 0.000000 seconds of processor time
Sleeping for 4 seconds
start = 0, end = 0
Slept for 0.000000 seconds of processor time
After computations, clock() returns 940000
================================================== ======================
The amount of processor time consumed by each sleep() call was
approximately zero; it was not anywhere near the wall clock time
consumed. This was very different from the results you got.
If you reply to this with another long screed full of insults, words
in all-caps, and repeated exclamation points, I will ignore you. If
you care to calm down and discuss technical issues, I'll be glad to
continue the discussion. It's up to you.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"