{
    "mode": "perldoc",
    "parameter": "Time::Stopwatch",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Time%3A%3AStopwatch/json",
    "generated": "2026-06-14T13:19:46Z",
    "synopsis": "use Time::Stopwatch;\ntie my $timer, 'Time::Stopwatch';\ndosomething();\nprint \"Did something in $timer seconds.\\n\";\nmy @times = map {\n$timer = 0;\ndosomethingelse();\n$timer;\n} 1 .. 5;",
    "sections": {
        "NAME": {
            "content": "Time::Stopwatch - Use tied scalars as timers\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Time::Stopwatch;\ntie my $timer, 'Time::Stopwatch';\n\ndosomething();\nprint \"Did something in $timer seconds.\\n\";\n\nmy @times = map {\n$timer = 0;\ndosomethingelse();\n$timer;\n} 1 .. 5;\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The Time::Stopwatch module provides a convenient interface to timing functions through tied\nscalars. From the point of view of the user, scalars tied to the module simply increase their\nvalue by one every second.\n\nUsing the module should mostly be obvious from the synopsis. You can provide an initial value\nfor the timers either by assigning to them or by passing the value as a third argument to tie().\n\nIf you have the module Time::HiRes installed, the timers created by Time::Stopwatch will\nautomatically count fractional seconds. Do *not* assume that the values of the timers are always\nintegers. You may test the constant \"Time::Stopwatch::HIRES\" to find out whether high resolution\ntiming is enabled.\n\nA note on timing short intervals\nTime::Stopwatch is primarily designed for timing moderately long intervals (i.e. several\nseconds), where the overhead imposed by the tie() interface does not matter. With Time::HiRes\ninstalled, it can nonetheless be used for even microsecond timing, provided that appropriate\ncare is taken.\n\n*   Explicitly initialize the timer by assignment. The first measurement taken before resetting\nthe timer will be a few microseconds longer due to the overhead of the tie() call.\n\n*   Always subtract the overhead of the timing code. This is true in general even if you're not\nusing Time::Stopwatch. (High-level benchmarking tools like Benchmark.pm do this\nautomatically.) See the code example below.\n\n*   Take as many measurements as you can to minimize random errors. The Statistics::Descriptive\nmodule may be useful for analyzing the data. This advice is also true for all benchmarking.\n\n*   Remember that a benchmark measures the time take to run the benchmark. Any generalizations\nto real applications may or may not be valid. If you want real world data, profile the real\ncode in real use.\n\nThe following sample code should give a relatively reasonable measurement of a the time taken by\na short operation:\n\nuse Time::HiRes;  # high resolution timing required\nuse Time::Stopwatch;\n\nuse Statistics::Descriptive;\nmy $stat = Statistics::Descriptive::Sparse->new();\n\ntie my $time, 'Time::Stopwatch';  # code timer\ntie my $wait, 'Time::Stopwatch';  # loop timer\n\nwhile ($wait < 60) {  # run for one minute\nmy $diff = 0;\n$time = 0; dowhatever(); $diff += $time;\n$time = 0;                $diff -= $time;\n$stat->adddata($diff);\n}\n\nprint(\"count: \", $stat->count(), \" iterations\\n\",\n\"mean:  \", $stat->mean(), \" seconds\\n\",\n\"s.d.:  \", $stat->standarddeviation(), \" seconds\\n\");\n\nNote that the above code includes the time of the subroutine call in the measurement.\n",
            "subsections": []
        },
        "BUGS": {
            "content": "Since tied scalars do not (yet?) support atomic modification, use of operators like \"$t++\" or\n\"$t *= 2\" on timers will cause them to lose the time it takes to fetch, modify and store the\nvalue. I *might* be able to get around this by overloading the return value of \"FETCH\", but I\ndoubt if it's worth the trouble. Just don't do that.\n\nThere is no way to force low-resolution timing if Time::HiRes has been installed. I'm not sure\nwhy anyone would want to, since int() will do just fine if you want whole seconds, but still..\n",
            "subsections": []
        },
        "CHANGE LOG": {
            "content": "1.00 (15 Mar 2001)\nExplicitly localized $SIG{DIE} when testing for Time::HiRes availability. Added \"A note\non timing short intervals\" to the POD documentation. Bumped version to 1, no longer beta.\n\n0.03 (27 Feb 2001)\nModified tests to give more information, reduced subsecond accuracy test to 1/10 seconds to\nallow for inaccurate select() implementations. Tweaked synopsis and README.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Time::HiRes, \"tie\" in perlfunc\n\nFor a higher-level approach to timing, try (among others) the modules Time::SoFar, Devel::Timer,\nor Benchmark. Also see the profiling modules Devel::DProf, Devel::SmallProf and Devel::OpProf.\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "Copyright 2000-2001, Ilmari Karonen. All rights reserved.\n\nThis library is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n\nAddress bug reports and comments to: perl@itz.pp.sci.fi\n",
            "subsections": []
        }
    },
    "summary": "Time::Stopwatch - Use tied scalars as timers",
    "flags": [],
    "examples": [],
    "see_also": []
}