I want to implement automatic benchmarking for the packages I load (some of which are my own). Right now my preamble takes 15 seconds to load, much longer than the rest of my document seems to take to compile.
For this I wanted to implement a remark from this answer, which recommended using etoolbox's patchcmmd to patch usepackage. As most of my package loading happens inside of my own packages, I wanted to use this approach on RequirePackage. I opted however to use xpatch's utilities, as I had read that the normal patchcmd can't deal with commands with optional arguments.
My best try so far is this (please don't mind the ugly automatic output via AfterEndPreamble):
\RequirePackage{fp}
\RequirePackage{xpatch}
\newcount\timer
\xpretocmd{\RequirePackage}{\pdfresettimer}{}{}
\xapptocmd{\RequirePackage}{
\timer=\pdfelapsedtime
\FPdiv\temp{\the\timer}{65.536}
\FPtrunc\temp\temp{3}
\AfterEndPreamble{\par{#1:} \temp}
}{}{}
% Testing
\RequirePackage{tikz}
\RequirePackage{mathtools}
\documentclass{article}
\begin{document}
\end{document}
But sadly this produces an Error:
! LaTeX Error: File `\timer.sty' not found.
My best guess is that this is because ProvideCommand has a second optional argument after the normal one and xpatchcmd can't handle that, causing ProvideCommand to consume the appended text as it's main argument. But I'm way out of my comfort zone here, and I have not managed to understand ProvideCommand via \show so far (and I assume there must be a better way of just patching a command with surrounding code anyway, without knowing its internal structure). Any help is greatly appreciated.


\TimeRequirePackagein a way that it can deal with the\RequirePackage[options]{package}[date]commands?\IfValueTFdeals well with the\RequirePackage[options]{package}case. I have a few of those\RequirePackagecommands with date in my files. In fact, I think this was the reason I initially tried the approach of patching the command via xpatch. – r0uv3n Apr 01 '21 at 15:37\RequirePackages, couldn't I just use\csname #1time \endcsname(with appropriate\expandafters) instead of\tempand then use differences in\pdfelapsedtimeinstead of resetting the pdf timer each time the command is invoked? Or is there some more fundamental obstacle to this? – r0uv3n Apr 01 '21 at 16:02