# How to determine the run time of a loop?

pdfTeX, provides two macros that can be used to determine the time elapsed from a run start. The first one determines the elapsed time in "scaled seconds", that means seconds divided by 65536.

\pdfelapsedtime


and the second one \pdfresettimer that resets the internal timer back to 0.

I got curious and ran the following code:

\documentclass{article}
\usepackage{fp,siunitx,ifthen}

\def\startTimer{\pdfresettimer}
\def\stopTimer{%
\the\pdfelapsedtime\,scaled seconds
\FPdiv\result{\the\pdfelapsedtime}{65536}
\FPmul\result{\result}{1000000} %microseconds
\FPround\result{\result}{6}\par
\result \si{\milli\second}\par}

\begin{document}
\startTimer
\newcount\n
\n=0
\loop%
\number\n, %
\ifnum\n<888%
\repeat% 3-4 scaled points
%% ifthen code
\newcounter{acount}
\whiledo{\value{acount}<888}{%
\stepcounter{acount}%
\theacount, } %5-6 scaled points average
\stopTimer
\end{document}


My question is: Is this a good way to measure the efficiency of the loops? Are there any other ways? Can you please post your run times and your machine for comparison?

My results: "loop" and "whiledo"

-
Your code only has one timer. –  Caramdir Jan 16 '11 at 17:58
I suspect that what slows down the old-fashioned \loop...\repeat is that you are using \texttt, rather than \theacount. The former needs quite a few expansions, whereas the latter is just one "very simple" expansion. –  Bruno Le Floch Jan 16 '11 at 17:59
@Bruno, I also wrote this in a now deleted answer, but Yiannis seems to have removed that code part in the mean time. –  Caramdir Jan 16 '11 at 18:03
run time: first loop about 45 scaled seconds (without \texttt); second loop is about 1760 scaled seconds. –  Caramdir Jan 16 '11 at 18:04
@Bruno @Caramdir sorry I edited while you were posting the delay was the texttt which I realized and edited the answer probably due to the loading of fonts? I run the code twice with the macros commented accordingly. –  Yiannis Lazarides Jan 16 '11 at 18:08

In this case, the \loop\repeat loop is going to be a little faster because it has slightly less work to do for the comparison and body. If the two loops were substantially different implementations for the same problem, then testing a single number of iterations may not yield the fastest way to do it. Testing several different numbers of iterations would be more instructive.