http://qs321.pair.com?node_id=286621

hotshot has asked for the wisdom of the Perl Monks concerning the following question:

Hello all!

I need to write in perl a function that will return the microsecond, in similar to C function gettimeofday() that returns a struct with microsecond (timeval.tv-usec). Anyone has a code suggestion or can refer me to a useful module for that?

Thanks

Hotshot

Replies are listed 'Best First'.
Re: Getting microseconds
by edan (Curate) on Aug 26, 2003 at 08:28 UTC
Re: Getting microseconds
by Zaxo (Archbishop) on Aug 26, 2003 at 08:36 UTC

    Actual time resolution to that accuracy is chancy on most platforms. As recommended, Time::HiRes is a good bet, but most platforms will not guarantee any absolute time better than the schedulers time slice, or jiffy. If you need better, check around for 'real time' kernels.

    After Compline,
    Zaxo

Re: Getting microseconds
by thunders (Priest) on Aug 26, 2003 at 14:23 UTC

    you could use Inline::C. I'm not really a C programmer, but something like this should print the result of that C call.

    print currentTimeMillis(); use Inline C => <<'END_OF_C_CODE'; #include <sys/time.h> long currentTimeMillis() { long t; struct timeval tv; gettimeofday(&tv, 0); t = tv.tv_usec; return t; } END_OF_C_CODE