in reply to Clocking Portion of Your Perl Code

Benchmark is probably what you're looking for. If you need sub-second timing, get Time::HiRes as well. Or you may want to look at Devel::Profiler and other modules in the Devel::* namespace.

Replies are listed 'Best First'.
Re^2: Clocking Portion of Your Perl Code
by salva (Canon) on Jun 12, 2005 at 12:00 UTC
    just a warning about the use of profiling modules to time things:

    perl profilers work running additional perl code between every instruction and subroutine call. That introduces overhead and affects performance and timing, sometimes in a no linear fashion. What looks faster under a profiler will actually run slower in normal execution.

    So, profilers are good to find hot spoots in code but not to compare different algorithms or implementations.

      That introduces overhead and affects performance and timing
      Dear salva,
      Thanks so much for your important comment!
      Does that also affect timing with Benchmark or Benchmark::Timer?
      If so what's the best way to time the portion of your code without having have to add overhead.
        You can usually trust Benchmark (I don't know about Benchmark::Timer)... but it's not perfect either: Benchmark.pm: Does subroutine testing order bias results?

        Sometimes, timing the full program (or a script specially written for the ocasion) from the shell with time is also a good solution.

Re^2: Clocking Portion of Your Perl Code
by Anonymous Monk on Jun 12, 2005 at 03:32 UTC
    In Benchmark module, I also mean that with 'timethese' etc method. I do have to _separate out_ the code that I want to measure the time. This is inconvenient. Is it possible to time those part without altering the overall structure of my code?

    Devel::Profiler only time the code as a whole not portion of it.
Re^2: Clocking Portion of Your Perl Code
by Anonymous Monk on Jun 12, 2005 at 03:22 UTC
    I thought Benchmark is only used for "comparing" subroutines, etc. I don't recall any method in Benchmark that does stand-alone time measurement.
      Check these methods in Benchmark:
      timethis timeit countit
      Regards,
      Edward