How to do fine grain performance measurement with mixed latex, luatex code? Let's say there are two blocks of code, executed one after another. The first one is in plain latex, and the second is luatex. I would like to know the real overhead of adding the second piece of code. A test case that comes to mind is doing a vpack on an existing vbox that was not originally set to the the size of its contents. How expensive is a vpack compared to vbox creation? That's something I am actually interested in knowing, especially for huge vboxes for instance. Here's my the code that needs performance timer insertions (at marked locations):
(Note: Ignore "Natural height" value of tempvbox, its incorrect as the height exceeds tex limit. You can of course see a correct value by reducing the number of paragraphs in blindtext from 1000 to something small, though that will also reduce the accuracy of ratio of two time deltas in consideration.)
% >> lualatex <filename>.tex
\documentclass[notitlepage,letterpaper]{article}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{printlen}
\setlength\parindent{0pt}
\uselengthunit{in}
\begin{document}
% Time1
\newsavebox{\tempvbox}
\setbox\tempvbox=\vbox to 4in{{\hsize=4in \noindent\blindtext[1000]}}
% Time2
\directlua{
tempvboxnatural = node.vpack(tex.getbox('tempvbox').head)
}%
% Time3
Set height of tempvbox: \directlua{tex.sprint("\csstring\%f in",(tex.getbox('tempvbox').height/tex.sp('1in')))}
Natural Height of tempvbox: \directlua{tex.sprint("\csstring\%f in",(tempvboxnatural.height/tex.sp('1in')))}
%Print Time2-Time1, and Time3-Time2 here
Time2-Time1: ??
Time3-Time2: ??
Partial contents of tempvbox:
\vsplit\tempvbox to 6in
\end{document}
Screenshot of output:
tex.sprint
in above code?tex.sprint(-2, ...)
andstring.format()
to format the string. Otherwise TeX might interpret some characters like %.tex.sprint(-2,string.format("\csstring\%f in",(tex.getbox('tempvbox').height....
os.gettimeofday()
to get the current time. Just get the time at the beginning and then at the end and subtract both.os.clock
overos.gettimeofday
here because it is not influenced by changes to the system time.os.clock
on LuaTeX while all other engines use gettimeofday. To make it more uniform, the next release will unify this to always use gettimeofday (even under LuaTeX). I still think thatos.clock
is better (especially since CPU time is much more useful for most benchmarking) but in this case consistency was more important. (See my comment at github.com/latex3/latex3/pull/796#issuecomment-678357951 for more about that)