Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
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
show 5 more comments

2 Answers 2

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.