After some help from here, the following code seems to be pretty close to the original primitive. We look at the pdflatex release notes (2005-08-01) first
\pdfelapsedtime is a read-only integer that (initially) returns the
amount of time passed since the start of this run. This amount is given
in `scaled seconds': the value 65536 counts as one second. If more time
has passed than 32767 seconds, (2^31)-1 will be returned.
\pdfresettimer updates the internal timer, such that subsequent calls to
\pdfelapsedtime will restart from 0.
Now, the try to reimplement in user space:
\directlua{pdfelapsedtimer_basetime=0}
os.clock() counts from the start of lua, so it pdfelapsedtimer_basetime is set to zero. No need to read the initial os.clock() value, it is supposed to be 0 anyway. From lua.org:
The os.clock function returns the number of seconds of CPU time for
the program. Its typical use is to benchmark a piece of code
If the user want to reset it, the pdflatex primitive is \pdfresettimer. In that case, we read out os.clock() and store it in pdfelapsedtimer_basetime to subtract the offset later.
\protected\def\pdfresettimer{\directlua{pdfelapsedtimer_basetime = os.clock()}}
Finally, the \pdfelapsedtime macro subtracts the offset to get seconds, multiplies by 65536 to get scaled seconds, adds 0.5 to allow the function math.floor proper rounding and returns an integer. We need an integer and not characters in the token stream, therefore, \numexpr is utilized.
\protected\def\pdfelapsedtime{\numexpr\directlua{tex.print(math.floor((os.clock()-pdfelapsedtimer_basetime)*65536+0.5))}\relax}
The following MWE is an implementation for the original question:
\documentclass{article}
\newcount\benchmarkcount
\long\def\tenfold#1{#1#1#1#1#1#1#1#1#1#1}
\long\edef\thousandfold#1{\tenfold{\tenfold{\tenfold{#1}}}}
\long\def\benchmark#1{%
\benchmarkcount\pdfelapsedtime
\thousandfold{#1}%
\typeout{\the\dimexpr\pdfelapsedtime sp-\benchmarkcount sp}%
}
\usepackage{lipsum}
\directlua{pdfelapsedtimer_basetime=0}
\protected\def\pdfresettimer{\directlua{pdfelapsedtimer_basetime = os.clock()}}
\protected\def\pdfelapsedtime{\numexpr\directlua{tex.print(math.floor((os.clock()-pdfelapsedtimer_basetime)*65536+0.5))}\relax}
\begin{document}
\benchmark{\lipsum}
\end{document}
\tenfold), the total time in seconds is the time in milliseconds taken by one copy of the code. [Then, there is a typo, I should've written "millisecond".] – Bruno Le Floch Dec 07 '11 at 09:12