Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Timing function in C

Reply
Thread Tools

Timing function in C

 
 
ericunfuk
Guest
Posts: n/a
 
      03-09-2007
Dear All...............

In the following java imp of stop-and-wait protocol, it creates a
timer, are there any similar functions in C that allows me to do this?


Thanks in advance.

//waiting for the ACK message, stop-and-wait , waiting for 10sec
t = new ACK_timer(10);
while (true)
{
if ( t.timer_out == true )break;

p_rcv = port_default.recv(false);

if ( p_rcv == null ) continue;

len_rcvd = (int)
p_rcv.getPropertyLong("HEADER_CONTENT_LENGTH", l);
message_rcv_id = p_rcv. getPropertyLong ("MESSAGE_ID", k);
//Verify the ACK message
if
( p_rcv.getProperty("HEADER_CONTENT_TYPE","EEE") == "ACK" &&
message_rcv_id == last_send_msg_id )
{
last_ack_msg_id = message_rcv_id ;
t.del_ACK_timer();
break;
}
}

next_send_msg_id = last_ack_msg_id + 1;

} while (true);
}



}

 
Reply With Quote
 
 
 
 
user923005
Guest
Posts: n/a
 
      03-09-2007
>From the C-FAQ:
19.37: How can I implement a delay, or time a user's response, with
sub-
second resolution?

A: Unfortunately, there is no portable way. V7 Unix, and derived
systems, provided a fairly useful ftime() function with
resolution up to a millisecond, but it has disappeared from
System V and POSIX. Other routines you might look for on your
system include clock(), delay(), gettimeofday(), msleep(),
nap(), napms(), nanosleep(), setitimer(), sleep(), times(), and
usleep(). (A function called wait(), however, is at least under
Unix *not* what you want.) The select() and poll() calls (if
available) can be pressed into service to implement simple
delays. On MS-DOS machines, it is possible to reprogram the
system timer and timer interrupts.

Of these, only clock() is part of the ANSI Standard. The
difference between two calls to clock() gives elapsed execution
time, and may even have subsecond resolution, if CLOCKS_PER_SEC
is greater than 1. However, clock() gives elapsed processor time
used by the current program, which on a multitasking system may
differ considerably from real time.

If you're trying to implement a delay and all you have available
is a time-reporting function, you can implement a CPU-intensive
busy-wait, but this is only an option on a single-user, single-
tasking machine as it is terribly antisocial to any other
processes. Under a multitasking operating system, be sure to
use a call which puts your process to sleep for the duration,
such as sleep() or select(), or pause() in conjunction with
alarm() or setitimer().

For really brief delays, it's tempting to use a do-nothing loop
like

long int i;
for(i = 0; i < 1000000; i++)
;

but resist this temptation if at all possible! For one thing,
your carefully-calculated delay loops will stop working properly
next month when a faster processor comes out. Perhaps worse, a
clever compiler may notice that the loop does nothing and
optimize it away completely.

References: H&S Sec. 18.1 pp. 398-9; PCS Sec. 12 pp. 197-8,215-
6; POSIX Sec. 4.5.2.


 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Timing function problem..... kevin Python 2 01-25-2007 04:45 PM
Timing a function object versus timeit Steven D'Aprano Python 2 11-04-2006 05:23 AM
Keypress irregularity: IE vs. FF function call timing? mens libertina Javascript 1 06-19-2006 02:55 AM
timing of a function Vasileios C++ 6 01-19-2004 02:17 PM
timing a function with timeit? (newbie problem) news.west.cox.net Python 1 08-28-2003 01:24 AM



Advertisments