The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Time::TimeTick - Keep a tally of times at different places in your program


  use Time::TimeTick;
  # Your code...
  timetick("Starting phase three");
  # More of your code...


Time::TimeTick provides a quick and convenient way of instrumenting a program to find out how long it took to reach various points. Just use the module and call the timetick() function whenever you want to mark the time at a point in your program. When the program ends, a report will be output giving the times at which each point was reached.

The times will be recorded using Time::HiRes::time() if Time::HiRes is available, otherwise time() will be used. (Since time() has one-second granularity this is unlikely to be useful.)


To disable the effect of timing with minimal modification to your program, just change ``use Time::TimeTick ...'' to ``no Time::TimeTick...''. The timetick() function will contain no instructions in order to maximize execution speed, and no report will be triggered.


You can customize the action of Time::TimeTick via options passed as key-value pairs in the use statement. Recognized keys are:


If true, do not put an initial entry in the report when the module is loaded.


If true, do not put a final entry in the report when the program terminates.


If set, replaces the default entry of ``Timeticker for <program> starting'' output initially (only if suppress_initial is not set.)


If set, replaces the default entry of ``Timeticker for <program> finishing'' output initially (only if suppress_final is not set.)


If true, report all times relative to the time that Time::TimeTick was loaded rather than the actual start of the program.


If set, should be a reference to a subroutine that will take as input a tag as passed to timetick() and return the actual tag to be used. Can be helpful for applying a lengthy transformation to every tag while keeping the calling code short.


If set, should be a reference to a subroutine that will take as input a list of time ticks for reporting. Each list element will be a reference to an array containing the time and the tag respectively. The default format_report callback is:

  sub { printf("%7.4f %s\n", @$_) for @_ }

If true, do not output a report when report() is called; just reset the time tick list instead.



Record the time at this point of the program and label it with the string $tag.


Output a report (unless suppress_report is set) and reset the time tick list.


Add a final time tick (unless suppress_final is set), and output a report. Called by default when the program finishes.


Time::TimeTick::timetick is exported to the caller. Note that Time::TimeTick::report is not exported; if you want to call it explicitly you will have to qualify the function name with the package name.


  use Time::TimeTick suppress_initial => 1;
  # ... picture intervening lines of code
  timetick("Phase 2");
  # ... picture more code
  timetick("Phase 3");
  # ... and yet more
  timetick("Phase 4");
  # Some time later, the program ends

Output from Time::TimeTick:

  0.7524 Phase 2
  0.7945 Phase 3
  0.8213 Phase 4
  0.8328 Timeticker for testprog finishing


Peter Scott, <>


Benchmark::Timer, Time::HiRes.


This module was originally written in a modified form and published in the book "Perl Medic" (Addison-Wesley, 2004): Readers of that book should note that the user interface is different from what appeared there.


Copyright(c) 2004 Peter Scott.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.