time problems

L

lucifer

hi,
if any body can tell me how to write a function that will create delay
according to the speed of processor
 
K

Keith Thompson

lucifer said:
if any body can tell me how to write a function that will create delay
according to the speed of processor

This can't be done in standard C. See question 19.37 in the C FAQ,
and try a newsgroup specific to your system.

It's not clear what you mean by "delay according to the speed of
processor". Do you want to delay for a longer period of time on
slower processors? There's no need to answer here, but be sure to
make it clear when you post to a more appropriate newsgroup.
 
L

lucifer

actually i am transmitting data over wireless medium so i have to
control the data ie slow it down thats i have to get a delay in the
transmitting function inversely to the speed of the the processor the
code i have been using is


void delay(int n)
{
clock_t start = clock();
while( ( ( (clock()-start)/CLOCKS_PER_SEC )*1000 )<n) ;
}

the n is calculated frm another function
 
F

Flash Gordon

lucifer wrote:

Provide context otherwise we don't know why you are posting this. Only
yesterday and the day before that and the day before that (and probably
today as well) instructions were posted together with the reason why you
need to provide context.

I believe you can either hit the preview button before writing your
reply or instead of using the reply button beneath the message you the
options button above to reveal a reply button that works properly.

Most of us do *not* use Google because it is a horrible interface. There
is no guarantee we have received the message you are replying to or, if
we have, that we have not forgotten what it said and have no convenient
way to view it.
actually i am transmitting data over wireless medium so i have to
control the data ie slow it down thats i have to get a delay in the
transmitting function inversely to the speed of the the processor the
code i have been using is

void delay(int n)
{
clock_t start = clock();
while( ( ( (clock()-start)/CLOCKS_PER_SEC )*1000 )<n) ;
}

the n is calculated frm another function

Good for you. Of course, if clock_t is an integral type and
CLOCKS_PER_SEC is integral (which it will be if clock_t is) then it
might not do quite what you want because it will throw away the
remainder of the division.

Also clock() returns processor time, *not* wall clock time, and in many
environments depending on loading this can be vastly different.

You don't check whether your call to clock() succeeds, it returns
(clock_t)-1 if it fails.

There is no guarantee as to the precision or accuracy of clock().

<OT>
If this is on a multi-tasking system you are burning processing cycles.
</OT>

For a better solution ask in groups dedicated to your system (or systems
of interest) since there is no good portable solution.
 
M

Mabden

Keith Thompson said:
This can't be done in standard C. See question 19.37 in the C FAQ,
and try a newsgroup specific to your system.

It's not clear what you mean by "delay according to the speed of
processor".

Of course it is. Dig out an old video game. I like Dragon Strike because
you get to ride on the back of the Dragons. Blue Dragon are Electric;
Red dragons are fire. Very cool game - ran very slow on my 486 (I
think - maybe even 386).

Finding it in my garage, I loaded onto a 400MHz AMD Whatever (PIII or
more) machine and it was crap! The opposing dragons flew rings around
me! It was totally unplayable!!

The poster is asking how to time a game so that it works well on old
processors, but doesn't become unplayable under modren* technology. The
standard of timing via the video rate is probably out-dated...

One needs to consider how to delay a game based on how fast it is
expected to run. This is one of the reasons I prefer to write games that
are turn-based. <Palm game plug omitted>

* I used "modren" to describe modern stuff that will surely and
obviously be replaced with faster stuff. Your cell phone might well be
the most modren available...
 
K

Keith Thompson

Mabden said:
Of course it is. Dig out an old video game. I like Dragon Strike because
you get to ride on the back of the Dragons. Blue Dragon are Electric;
Red dragons are fire. Very cool game - ran very slow on my 486 (I
think - maybe even 386).

I see what you mean. It just wasn't clear to me that that's what the
original poster meant. I would describe what you're talking about as
delaying for a particular real time interval *regardless* of the speed
of the processor. If you do it with a busy loop, you have to
calibrate the loop to the processor speed, but if you do it with a
system call that delays for a specified real time interval, the
processor speed is irrelevant (at least to the caller).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,079
Latest member
ElidaWarin

Latest Threads

Top