another question about time()

Derek Parnell derek at psych.ward
Wed Apr 26 17:52:21 PDT 2006


On Wed, 26 Apr 2006 22:09:05 +0200, Abby (J.P.) wrote:

> Hello, I'm trying to make a ping utility, I made a working ICMP request 
> (I tested it with Ethereal, I get a reply from the server). But I wonder 
> how much precise are the std.c.time.time() or the std.date.time(), 
> because I always find a ping of 0ms (I know that it must be 1000 ticks 
> per seconds, but I'm not sure, this really is).
> Here is my code :
> 
> char[] host = "www.microsoft.com"
> InternetHost targetHost;
> targetHost.getHostByName(host);
> 
> InternetAddress targetAddress = new 
> InternetAddress(targetHost.addrList[0], InternetAddress.PORT_ANY);
> 
> Socket pingingSocket = new Socket(AddressFamily.INET, SocketType.RAW, 
> ProtocolType.ICMP);
> 
> //pingingSocket.connect(targetAddress);
> //long start_time = getUTCtime();
> int start_time;
> std.c.time.time(&start_time);
> 
> pingingSocket.sendTo(PING_REQUEST, targetAddress);
> 
> char[] response;
> pingingSocket.receive(response);
> 
> //long end_time = getUTCtime();
> int end_time;
> std.c.time.time(&end_time);
> 
> // char [] str_ping = std.string.toString((end_time - start_time));
> char [] str_ping = std.string.toString((end_time - start_time)/2);
> pingingSocket.close();
> printf("ping : " ~ str_ping);
> 
> As you can see, I tried both std.c.time and std.date functions, but 
> str_ping is always 0. Did I done something wrong ? Should I put the 
> start_time before the resolving getHostByName() ?

You might be better to use another undocumented module 'std.perf'.

For example ...

   import std.perf;
   . . .
   ProcessTimesCounter counter = new ProcessTimesCounter();
   counter.start();

   . . .  the stuff you want to time . . .

   counter.stop();
   writefln("Duration: %s microseconds", counter.microseconds);


-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocracy!"
27/04/2006 10:50:33 AM



More information about the Digitalmars-d-learn mailing list