[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[Libevent-users] optimize evutil_gettimeofday()'s Windows implementation
- To: libevent-users@xxxxxxxxxxxxx
- Subject: [Libevent-users] optimize evutil_gettimeofday()'s Windows implementation
- From: Dave Hart <davehart@xxxxxxxxx>
- Date: Sat, 16 Jun 2012 00:17:05 +0000
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: libevent-users-outgoing@xxxxxxxx
- Delivered-to: libevent-users@xxxxxxxx
- Delivery-date: Fri, 15 Jun 2012 20:17:34 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:reply-to:from:date:message-id:subject:to:content-type; bh=ErgwKKKGjLWY0vHI+kk4JHvhTHoz4cJIbDz6QxC9ad0=; b=CSYbrNdL72kzehF5meNGfzUd8xWl6KCNOBb0olsGCSUrqzHsi8rMrGOlDENMEmDbyq 5+gkoZbKsBoBDxO4gGo3HqD8BYRE8MBAkikwXE2cRhkm20vVabHOp+h4D1nUFnFwUQP2 l9BO1JZFggxHW2VRPNT1vnk4O0C0DQw76b1PQnIHhQ9jeP5Fx7TfT1P2hJeF6s38SmgW MpumJA2LjD+nMSzMIsz3DPhoTjLzMK3LU8Hj/jY8gEEaS+1Dc6V4K/S2d3sThPxD2DzL Rgiw7GIetfA1jjec5TOH3mJaig/fcGXbAveiVigudwvgBk9KBhbQoJ08ApKYeu167GjS rScQ==
- Reply-to: libevent-users@xxxxxxxxxxxxx
- Sender: owner-libevent-users@xxxxxxxxxxxxx
The code for evutil_gettimeofday() on Windows is:
/* Conversion logic taken from Tor, which in turn took it
* from Perl. GetSystemTimeAsFileTime returns its value as
* an unaligned (!) 64-bit value containing the number of
* 100-nanosecond intervals since 1 January 1601 UTC. */
#define EPOCH_BIAS U64_LITERAL(116444736000000000)
#define UNITS_PER_SEC U64_LITERAL(10000000)
#define USEC_PER_SEC U64_LITERAL(1000000)
#define UNITS_PER_USEC U64_LITERAL(10)
union {
FILETIME ft_ft;
ev_uint64_t ft_64;
} ft;
if (tv == NULL)
return -1;
GetSystemTimeAsFileTime(&ft.ft_ft);
if (EVUTIL_UNLIKELY(ft.ft_64 < EPOCH_BIAS)) {
/* Time before the unix epoch. */
return -1;
}
ft.ft_64 -= EPOCH_BIAS;
tv->tv_sec = (long) (ft.ft_64 / UNITS_PER_SEC);
tv->tv_usec = (long) ((ft.ft_64 / UNITS_PER_USEC) % USEC_PER_SEC);
return 0;
Note it ends with three 64-bit divisions. Given the routine can be
(though shouldn't be) a hot path, a micro-optimization seems worth
considering:
ft.ft_64 -= EPOCH_BIAS;
tv->tv_sec = (long) (ft.ft_64 / UNITS_PER_SEC);
tv->tv_usec = (long) ((ft.ft_64 - (UNITS_PER_SEC * tv->tv_sec)) /
UNITS_PER_USEC);
That substitutes a multiplication followed by subtraction in place of
the modulo 1 million. While I haven't tested it, I believe mul + sub
will be faster than modulo (division). I'm not actually using
libevent on Windows at this point.
Cheers,
Dave Hart
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users in the body.