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.


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

I got curious and ran the following code:


 \the\pdfelapsedtime\,scaled seconds
 \FPmul\result{\result}{1000000} %microseconds
 \result \si{\milli\second}\par}

   \advance\n by1
     \number\n, %  
\repeat% 3-4 scaled points
%% ifthen code 
 \theacount, } %5-6 scaled points average

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

  • 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

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

| improve this answer | |

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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