9

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%
   \advance\n by1
     \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" alt text and "whiledo" alt text

Caramdir
  • 89,023
  • 26
  • 255
  • 291
yannisl
  • 117,160
  • 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. – yannisl Jan 16 '11 at 18:08
  • @Caramdir I only used one timer as it appears that once you start running the program you cannot reset the timer and start again. But maybe I am wrong. – yannisl Jan 16 '11 at 18:16
  • @Yiannis: I think that just inserting \stopTimer and \startTimer between the two loops works. --- I get a similar result as Caramdir (factor of ~40 between the two). – Bruno Le Floch Jan 16 '11 at 18:17
  • @Bruno: yes it works but I get results that are very close for both (see edit). – yannisl Jan 16 '11 at 18:24
  • @Yiannis: On what machine are you running this? On a normal desktop computer it seems unlikely that the code runs that much faster unless there is something that seriously slows down my computer. – Caramdir Jan 16 '11 at 18:38
  • @Caramdir Windows Vista - 32bit 4.00 GB RAM 2.4 GHz – yannisl Jan 16 '11 at 19:27

2 Answers2

5

A general rule in timing statistics: do not trust any value below 1 full second.

Taco Hoekwater
  • 13,724
  • 43
  • 67
3

In this case, I'd say that's a reasonable way to measure the efficiency of loops. The body of each loop takes constant time as does the comparison. So the total time is just the combined time for the comparison and the body times the number of loop iterations.

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.

TH.
  • 62,639