*! version 1.3.0 04okt2022

timeit

timeit -- Easy to use single line version of timer on/off, supports incremental timing

Syntax

timeit
integer
[name]
:
cmd

Description

timeit # : cmd will store the runtime of cmd in timer # and r(t#)

timeit # name : cmd will store the runtime of cmd in timer # and r(t#) and r(name)

Remarks

timeit # : cmd is functionally equivalent to

. timer on #
. cmd
. timer off #

If your cmd leads to an error, the timer will stop running. Hitting break will also stop the timer.

The time stored in timer #, r(t#) will always be total accumulated time of that timer.

E.g. if timer 1 was at 5s before and ran for 2s during -timeit 1 name:-, then timer 1 will be 7s and name = 7

The time stored in r(delta_t#) and optionally r(delta_name) will be the time spent in this run.

E.g. if timer 1 was at 5s before and ran for 2s during -timeit 1 name:-, then r(delta_t1) = 2

The time optionally stored in r(name) will be the total accumulated time of that named timer.

E.g. if timer regression was at 5s before and ran for 2s during -timeit 1 regression:-, then r(regression) = 7

Any mistakes are my own.

Examples

. sysuse auto, clear

. timer clear

** Run first timer

. timeit 1: reg price mpg

. di r(t1)

. local firstRun = r(t1)

** Run second timer (same number though)

. timeit 1 fullRunTime: reg price mpg trunk weight length, r

** Show total, named and incremental runtime (plus actual r-results from reg)

. return list

** Verify incremental runtime

. di r(fullRunTime) - `firstRun'

** Verify we get same outcome with timer list

. timer list

Stored results

timeit stores the following in r():

Scalars

r(t#) Current value of timer

r(delta_t#) Incremental value of timer

r(name) Current value of timer

r(delta_name) Incremental value of named timer

Author

Jesse Wursten
Faculty of Economics and Business
KU Leuven
jesse.wursten@kuleuven.be

Special thanks go to Daniel Klein for his suggestions and code snippets that vastly improved this command.