Velocity Reviews > Another Tricky Problem I am Messing With (Not Homework)

# Another Tricky Problem I am Messing With (Not Homework)

joebenjamin
Guest
Posts: n/a

 09-10-2007
This is a problem I was trying to help a few friends figure out for fun. I
here. Here is what we want the output to be:
Need to read in a time period from the keyboard (for example 1.2 seconds,
3.4 seconds, or 8.37 seconds). Once the time has been read in, we want to
print out the word “TICK” and then wait the designated time period and
print the word “TICK” again. Repeat until there are 6 “TICK”s on the
screen. It would be real neat if we knew how to variate between TICK and
TOCK, however this might be the output.

Enter a time ==> 2.27
TICK <wait 2.27 seconds>
TICK <wait 2.27 seconds>
TICK <wait 2.27 seconds>
TICK <wait 2.27 seconds>
TICK <wait 2.27 seconds>

Here is what I started, I am lost because I am a newbie lol... This is
wrong any ideas?

Code:
```
#include <stdio.h>
#include <conio.h>
#include <time.h>

int main( )

{
clock_t start, end;
float   total_time;

int i;
int j;
int k;
int timer;

printf("Enter any time in seconds\n ");
scanf ("%i", &timer);
getchar ();

printf( "Start timing\n" );
start = clock();

for ( i=0; i<5000; i++ )
for ( j=0; j<1000; j++ )
for ( k=0; k<100; k++ );

end = clock();

total_time = ( end - start ) / CLK_TCK;
printf("TICK\n");
printf("TICK\n");
printf("TICK\n");
printf("TICK\n");
printf("TICK\n");
printf("TICK\n");
printf( "\nTotal Time Elapsed : %0.3f seconds\n", total_time );

getch();
}```

--

Miguel Guedes
Guest
Posts: n/a

 09-10-2007
joebenjamin wrote:
> This is a problem I was trying to help a few friends figure out for fun. I
> here. Here is what we want the output to be:
> Need to read in a time period from the keyboard (for example 1.2 seconds,
> 3.4 seconds, or 8.37 seconds). Once the time has been read in, we want to
> print out the word â€śTICKâ€ť and then wait the designated time period and
> print the word â€śTICKâ€ť again. Repeat until there are 6 â€śTICKâ€ťs on the
> screen. It would be real neat if we knew how to variate between TICK and
> TOCK, however this might be the output.
>

Had nothing to do so I decided to give it a shot...

Made the code as simpler as I could; hopefully you'll understand what the code
does, as I find it self-explanatory.

It's been awhile since I've coded in C (since the late 80's) and am not sure
whether I've used any non-standard C features. Hopefully some of the regulars
here can spot'em and correct'em if I have.

#include <stdio.h>
#include <conio.h>
#include <time.h>

int main( )
{
clock_t start, target, end;
float timer;
int i, j;
char msg[][5] = {"TICK", "TOCK"};

do
{
printf("\nEnter any time in seconds: ");
scanf ("%f", &timer);

i = j = 0;

printf( "Start timing\n" );

start = clock();
while(i++ < 6)
{
target = clock() + (clock_t)(timer*(float)CLOCKS_PER_SEC);

while(clock() < target);

printf("%s ", msg[j]);

if(++j > 1)
j = 0;
}
end = clock() - start;

printf( "\nTotal Time Elapsed : %0.3f seconds, %0.3f/iteration\n",
(float)end/1000, (float)end/((i-1)*1000));

printf("Another go? y/[n] ");
} while(getch() == 'y');
}

Bart van Ingen Schenau
Guest
Posts: n/a

 09-10-2007
Miguel Guedes wrote:

<snip>
> {
> target = clock() + (clock_t)(timer*(float)CLOCKS_PER_SEC);
>
> while(clock() < target);

Although this is a standard conforming way to wait for a certain time
period, it is actually not a very friendly way on a multi-user system.
This loop probably consumes 100% CPU time while it is doing nothing.

For this kind of task, it is advisable to look for a implementation
specific method. Functions like Sleep (Windows) and sleep/usleep (unix)
come to mind.

Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/

Keith Thompson
Guest
Posts: n/a

 09-10-2007
Bart van Ingen Schenau <(E-Mail Removed)> writes:
> Miguel Guedes wrote:
> <snip>
>> {
>> target = clock() + (clock_t)(timer*(float)CLOCKS_PER_SEC);
>>
>> while(clock() < target);

>
> Although this is a standard conforming way to wait for a certain time
> period, it is actually not a very friendly way on a multi-user system.
> This loop probably consumes 100% CPU time while it is doing nothing.

And it doesn't necessarily do what you want it to do. The clock()
function returns an indication of CPU time, not real time. So if, for
example, your program is getting 25% of the CPU, the loop will wait 4
times as long as you probably want it to (while wasting 25% of the CPU
doing nothing particularly useful).

> For this kind of task, it is advisable to look for a implementation
> specific method. Functions like Sleep (Windows) and sleep/usleep (unix)
> come to mind.

Indeed. Delaying for a specified time interval is one of those things
that cannot be done well using only standard C, but that can probably
be done *very* well using some system-specific interface.

(It would have been easy enough for the standard to define a sleep()
function, but applications that need that functionality almost always
need other functionality that can't be define so easily in a portable
interface.)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"

Willem
Guest
Posts: n/a

 09-11-2007
Keith wrote:
) Indeed. Delaying for a specified time interval is one of those things
) that cannot be done well using only standard C, but that can probably
) be done *very* well using some system-specific interface.

A good bet would probably be the POSIX standard.
select(), for example, can be used to sleep for a specified time interval.

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

Bill Reid
Guest
Posts: n/a

 09-12-2007

Keith Thompson <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Bart van Ingen Schenau <(E-Mail Removed)> writes:
> > Miguel Guedes wrote:
> > <snip>
> >> {
> >> target = clock() + (clock_t)(timer*(float)CLOCKS_PER_SEC);
> >>
> >> while(clock() < target);

> >
> > Although this is a standard conforming way to wait for a certain time
> > period, it is actually not a very friendly way on a multi-user system.
> > This loop probably consumes 100% CPU time while it is doing nothing.

>
> And it doesn't necessarily do what you want it to do. The clock()
> function returns an indication of CPU time, not real time.

Really? Seems to work to a few thousandths of a second
here (note that _sleep() is my development package version of
a sleep/wait/delay function for Windows, and CLK_TCK
is the macro for determining the number of seconds from
clock()):

#include <stdio.h> /* required to breathe */
#include <time.h> /* required for clock functions */
#include <dos.h> /* required for "sleep" function */

int main(void) {
int inc;
clock_t start,end;

for(inc=1;inc<5;inc++) {
printf("Sleeping for %d seconds\n",inc);
start=clock();
_sleep(inc);
end=clock();
printf("Slept for %f seconds\n",((end-start)/CLK_TCK));
}
}

Sleeping for 1 seconds
Slept for 1.000000 seconds
Sleeping for 2 seconds
Slept for 2.000000 seconds
Sleeping for 3 seconds
Slept for 3.000000 seconds
Sleeping for 4 seconds
Slept for 4.000000 seconds

Hey, that appears to be "perfect timing"! Before when I ran
this some of the clock() timings were off by about 0.0002 to
0.0005 seconds; maybe clock() IS dependant on CPU usage...

As usual, I'm confused...

> So if, for
> example, your program is getting 25% of the CPU, the loop will wait 4
> times as long as you probably want it to (while wasting 25% of the CPU
> doing nothing particularly useful).

Actually, I would have just thought that you'd be kinda chasing
your CPU cycles around each other and wind up with a mess, as
was previously mentioned...

> > For this kind of task, it is advisable to look for a implementation
> > specific method. Functions like Sleep (Windows) and sleep/usleep (unix)
> > come to mind.

Yes, I think just about all compilers for general purpose computers
allow you to call in some way a system timer for suspending your program
for a period of time...

> Indeed. Delaying for a specified time interval is one of those things
> that cannot be done well using only standard C, but that can probably
> be done *very* well using some system-specific interface.

Yes, since some type of timer and process suspension/activation is
needed for most general purpose computer OSs...

> (It would have been easy enough for the standard to define a sleep()
> function, but applications that need that functionality almost always
> need other functionality that can't be define so easily in a portable
> interface.)

Portable to what? Embedded systems again? I'm surprised they
stooped so low as to include something to print to a "screen"...

---
William Ernest Reid

Keith Thompson
Guest
Posts: n/a

 09-12-2007
"Bill Reid" <(E-Mail Removed)> writes:
> Keith Thompson <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Bart van Ingen Schenau <(E-Mail Removed)> writes:
>> > Miguel Guedes wrote:
>> > <snip>
>> >> {
>> >> target = clock() + (clock_t)(timer*(float)CLOCKS_PER_SEC);
>> >>
>> >> while(clock() < target);
>> >
>> > Although this is a standard conforming way to wait for a certain time
>> > period, it is actually not a very friendly way on a multi-user system.
>> > This loop probably consumes 100% CPU time while it is doing nothing.

>>
>> And it doesn't necessarily do what you want it to do. The clock()
>> function returns an indication of CPU time, not real time.

>
> Really?

Yes, really.

C99 7.23.2.1p3:

The clock function returns the implementation's best approximation
to the processor time used by the program since the beginning of
an implementation-defined era related only to the program
invocation. To determine the time in seconds, the value returned
by the clock function should be divided by the value of the macro
CLOCKS_PER_SEC. If the processor time used is not available or its
value cannot be represented, the function returns the value
(clock_t)(-1).

> Seems to work to a few thousandths of a second
> here (note that _sleep() is my development package version of
> a sleep/wait/delay function for Windows, and CLK_TCK
> is the macro for determining the number of seconds from
> clock()):
>
> #include <stdio.h> /* required to breathe */
> #include <time.h> /* required for clock functions */
> #include <dos.h> /* required for "sleep" function */
>
> int main(void) {
> int inc;
> clock_t start,end;
>
> for(inc=1;inc<5;inc++) {
> printf("Sleeping for %d seconds\n",inc);
> start=clock();
> _sleep(inc);
> end=clock();
> printf("Slept for %f seconds\n",((end-start)/CLK_TCK));
> }
> }
>
> Sleeping for 1 seconds
> Slept for 1.000000 seconds
> Sleeping for 2 seconds
> Slept for 2.000000 seconds
> Sleeping for 3 seconds
> Slept for 3.000000 seconds
> Sleeping for 4 seconds
> Slept for 4.000000 seconds
>
> Hey, that appears to be "perfect timing"! Before when I ran
> this some of the clock() timings were off by about 0.0002 to
> 0.0005 seconds; maybe clock() IS dependant on CPU usage...
>
> As usual, I'm confused...

I have no idea how your '_sleep' function works. I suspect that
either '_sleep' delays for a specified interval of CPU time, or your
program is using exactly 1 second of CPU time per second of real time.
But if _sleep(4) consumes a full 4 seconds of CPU time doing nothing.

Note that clock_t could be signed, unsigned, or floating-point, and
CLK_TCK isn't defined in standard C; the correct macro is
CLOCKS_PER_SEC.

[...]

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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"

buuuuuum
Guest
Posts: n/a

 09-12-2007
On 11 set, 23:15, Keith Thompson <(E-Mail Removed)> wrote:
> "Bill Reid" <(E-Mail Removed)> writes:
> > Keith Thompson <(E-Mail Removed)> wrote in message
> >news:(E-Mail Removed)...
> >> Bart van Ingen Schenau <(E-Mail Removed)> writes:
> >> > Miguel Guedes wrote:
> >> > <snip>
> >> >> {
> >> >> target =clock() + (clock_t)(timer*(float)CLOCKS_PER_SEC);

>
> >> >> while(clock() < target);

>
> >> > Although this is a standard conforming way to wait for a certain time
> >> > period, it is actually not a very friendly way on a multi-user system.
> >> > This loop probably consumes 100% CPU time while it is doing nothing.

>
> >> And it doesn't necessarily do what you want it to do. Theclock()
> >> function returns an indication of CPU time, not real time.

>
> > Really?

>
> Yes, really.
>
> C99 7.23.2.1p3:
>
> The clock function returns the implementation's best approximation
> to the processor time used by the program since the beginning of
> an implementation-defined era related only to the program
> invocation. To determine the time in seconds, the value returned
> by theclockfunction should be divided by the value of the macro
> CLOCKS_PER_SEC. If the processor time used is not available or its
> value cannot be represented, the function returns the value
> (clock_t)(-1).

this mean that if i have a program that count execution's time and run
it several times it should output always the same value? because I
think it should take always the same processor time to run, if it
doesn't have i/o for example

but I tried some code here and even if I use sleep(), what I think
would make the program to doesn't use the processor time, the program
outputs the correct value

Bill Reid
Guest
Posts: n/a

 09-12-2007

Keith Thompson <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Bill Reid" <(E-Mail Removed)> writes:
> > Keith Thompson <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >> Bart van Ingen Schenau <(E-Mail Removed)> writes:
> >> > Miguel Guedes wrote:
> >> > <snip>
> >> >> {
> >> >> target = clock() + (clock_t)(timer*(float)CLOCKS_PER_SEC);
> >> >>
> >> >> while(clock() < target);
> >> >
> >> > Although this is a standard conforming way to wait for a certain time
> >> > period, it is actually not a very friendly way on a multi-user

system.
> >> > This loop probably consumes 100% CPU time while it is doing nothing.
> >>
> >> And it doesn't necessarily do what you want it to do. The clock()
> >> function returns an indication of CPU time, not real time.

> >
> > Really?

>
> Yes, really.
>
> C99 7.23.2.1p3:
>
> The clock function returns the implementation's best approximation
> to the processor time used by the program since the beginning of
> an implementation-defined era related only to the program
> invocation. To determine the time in seconds, the value returned
> by the clock function should be divided by the value of the macro
> CLOCKS_PER_SEC. If the processor time used is not available or its
> value cannot be represented, the function returns the value
> (clock_t)(-1).

Well, yeah, it's supposed to tell you how long the program has
been running. On my system the program running time is counted
in milliseconds starting from zero as soon as the program starts.
On some systems, this timing service is not available, so it returns
-1...

What I was really getting at was your statement that you
snipped:

> So if, for
> example, your program is getting 25% of the CPU, the loop will wait 4
> times as long as you probably want it to (while wasting 25% of the CPU
> doing nothing particularly useful).

At the very least, this should depend on the system, and at the worst,
may be conflating two values which aren't related to each other on any
system that supports stuff like clock() and sleep()...

> > Seems to work to a few thousandths of a second
> > here (note that _sleep() is my development package version of
> > a sleep/wait/delay function for Windows, and CLK_TCK
> > is the macro for determining the number of seconds from
> > clock()):
> >
> > #include <stdio.h> /* required to breathe */
> > #include <time.h> /* required for clock functions */
> > #include <dos.h> /* required for "sleep" function */
> >
> > int main(void) {
> > int inc;
> > clock_t start,end;
> >
> > for(inc=1;inc<5;inc++) {
> > printf("Sleeping for %d seconds\n",inc);
> > start=clock();
> > _sleep(inc);
> > end=clock();
> > printf("Slept for %f seconds\n",((end-start)/CLK_TCK));
> > }
> > }
> >
> > Sleeping for 1 seconds
> > Slept for 1.000000 seconds
> > Sleeping for 2 seconds
> > Slept for 2.000000 seconds
> > Sleeping for 3 seconds
> > Slept for 3.000000 seconds
> > Sleeping for 4 seconds
> > Slept for 4.000000 seconds
> >
> > Hey, that appears to be "perfect timing"! Before when I ran
> > this some of the clock() timings were off by about 0.0002 to
> > 0.0005 seconds; maybe clock() IS dependant on CPU usage...
> >
> > As usual, I'm confused...

>
> I have no idea how your '_sleep' function works. I suspect that
> either '_sleep' delays for a specified interval of CPU time,

No possibility it just delays execution of the program for a
specified amount of time regardless of the "CPU time"?

> or your
> program is using exactly 1 second of CPU time per second of real time.

Since it is nominally a "multi-tasking" single-processor system it
must be sharing that CPU time with all the other programs I'm running,
but that doesn't seem to affect the perceived time as the program
runs or timings returned by clock() much. For example, I'll go ahead
and run it while doing a heavy download from the net and starting
up another big program:

Sleeping for 1 seconds
Slept for 1.001000 seconds
Sleeping for 2 seconds
Slept for 2.000000 seconds
Sleeping for 3 seconds
Slept for 3.003000 seconds
Sleeping for 4 seconds
Slept for 4.004000 seconds

OK, like I said, sometimes it's off by a few thousandths of a second,
but it still managed to sneak in there while everything else was happening
and my second-hand watch perception was that the timings were exactly

> But if _sleep(4) consumes a full 4 seconds of CPU time doing nothing.

I think on a modern GUI single-processor system the CPU is always
doing SOMETHING, but the point of the "sleep" stuff is that my program
does NOTHING for a specified period of time...

> Note that clock_t could be signed, unsigned, or floating-point, and
> CLK_TCK isn't defined in standard C; the correct macro is
> CLOCKS_PER_SEC.

Yeah, I noticed that, but for this development package, they
use a different name for the same macro, which in this case is
a value supporting millisecond granularity...

---
William Ernest Reid

Richard Bos
Guest
Posts: n/a

 09-12-2007
"Bill Reid" <(E-Mail Removed)> wrote:

> Keith Thompson <(E-Mail Removed)> wrote in message
> > Bart van Ingen Schenau <(E-Mail Removed)> writes:
> > > Miguel Guedes wrote:
> > >> {
> > >> target = clock() + (clock_t)(timer*(float)CLOCKS_PER_SEC);
> > >>
> > >> while(clock() < target);
> > >
> > > Although this is a standard conforming way to wait for a certain time
> > > period, it is actually not a very friendly way on a multi-user system.
> > > This loop probably consumes 100% CPU time while it is doing nothing.

> >
> > And it doesn't necessarily do what you want it to do. The clock()
> > function returns an indication of CPU time, not real time.

>
> Really? Seems to work to a few thousandths of a second
> here (note that _sleep() is my development package version of
> a sleep/wait/delay function for Windows, and CLK_TCK
> is the macro for determining the number of seconds from
> clock()):

Seems is correct. It may do so under MS-DOS, when no other program is
running, but it isn't the right function to use for most systems.

> > > For this kind of task, it is advisable to look for a implementation
> > > specific method. Functions like Sleep (Windows) and sleep/usleep (unix)
> > > come to mind.

>
> Yes, I think just about all compilers for general purpose computers
> allow you to call in some way a system timer for suspending your program
> for a period of time...

Yup. And that's the best way to handle this. Busy-looping is not, unless

Richard