Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Get Exact Time w/out Modules

by Anonymous Monk
on Nov 17, 2003 at 18:28 UTC ( #307772=perlquestion: print w/replies, xml ) Need Help??

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

I have a question similar to that presented in this post.

I need to be able to find out how long it took to do something, but very precisely. So simply getting time(); before and after I call my subroutine won't work--I need accuracy down to, say, milliseconds.

I think this could be done with Time::HiRes, but I too am in a situation where I can't use modules. Can anyone help?

Replies are listed 'Best First'.
Re: Get Exact Time w/out Modules
by BrowserUk (Patriarch) on Nov 17, 2003 at 20:16 UTC

    With your code running on proprietory hardware, there is no way for us to predict what facilities may be available to perform this task.

    Most CPU's have very accurate hardware timers, and most OS's provide means to access them.

    Whether such facilities are available on the embedded system in question is something only you can answer.

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail

      Well, the device runs embedded Linux on a PPC processor, if that helps.
Re: Get Exact Time w/out Modules
by simonm (Vicar) on Nov 17, 2003 at 18:48 UTC
    I can't use modules

    Why not? What's the specific restriction you're dealing with? Site policy? Lack of install permissions? Lack of a compiler? Or... what?

    If it's just a distribution problem, consider downloading Time::HiRes and copying bits of its source code into your program...

      The code is going to be running on an proprietary embedded device. To add software to the image that it runs requires me to pay a hefty sum of money. It has perl already, but no modules.

        If you can't compile libraries, I think you're limited to the time() function and its one second accuracy. (You might be able to qx() out to a command-line program that returned the time with hundredths of a second precision, but that'd probably take long enough that it would throw off your timing.

        Update: Or, as mpeppler points out, you could use syscall(). It sounds like your manufacturer probably didn't include the headers or, but if they did this would be a good workaround.

        then you should consider getting rid of this proprietary device and going for something else. this kind of restrictiveness is like have a 10 ton block of cement chained to your ankle; you will never be able to get anywhere or do anything while you are still tied to it!
Re: Get Exact Time w/out Modules
by ehdonhon (Curate) on Nov 17, 2003 at 20:31 UTC

    So you have Perl, but no modules. I suspect that you will soon begin to conclude the Perl is a pain to program with. If you aren't going to be able to take advantage of the CPAN, then there seems to be very little sense in having Perl installed at all. It sounds to me like it would be much more to your advantage to program with a lower-level langauge, like C.

      So you have Perl, but no modules. I suspect that you will soon begin to conclude the Perl is a pain to program with
      I'll have to disagree here - while CPAN is incredibly useful, you're still going to be much more productive coding in perl than in C even without access to it, and by using the syscall() built-in you can achieve pretty much anything that you need for low-level system functions. It's much more work than using various perl modules from CPAN, but it should get you there.


      For the most part perl covers our needs wonderfully...cases like this are the exception. We don't have a C compiler either (they make you buy an SDK for that), so perl is more or less our only choice.
        If you don't have a C compiler, then you are going to have a hard time solving this problem. Presumably, the operating system has a function that give the time with a higher accuracy than the Perl builtin time(). Calling the C API from Perl requires an XS module compiled by a C compiler.

        The Time::HiRes module provides an interface to the gettimeofday function that will do the job. But you can't compile it because you don't have a C compiler. And can't install Perl modules.

Re: Get Exact Time w/out Modules
by Paul Smith (Initiate) on Nov 17, 2003 at 21:54 UTC
    Well, if you're wanting to find out how long it takes for, say, performance profiling, you could try doing the 'something' lots of times - eg do it 1000 times, call time() before and after, and the difference in seconds for doing it 1000 times is going to be roughly the same as doing it once in milliseconds
Re: Get Exact Time w/out Modules
by TomDLux (Vicar) on Nov 18, 2003 at 22:53 UTC

    You have to pay to have 'standard software' installed, I guess because this imposes a cost on all aspects of the system.

    Why not simply make Time::HiRes part of your application. If @INC does not include the current directory, begin your program with use lib '.';.

    Update: If you can't compile it for the platform, that's a complication. But if you have the technology, pre-compile what you have to, and just make it all local.


      Time::HiRes contains an XS component which requires a C-compiler, header files etc.

      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://307772]
Approved by Thelonius
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2022-05-21 16:00 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (76 votes). Check out past polls.