From 40891eeadc9a12dc1b78401e4ed4351a4729d6d2 Mon Sep 17 00:00:00 2001 From: Yuval Tassa Date: Thu, 13 Mar 2025 06:25:59 -0700 Subject: [PATCH] Time both first and second compilation in `sample/compile.cpp`. PiperOrigin-RevId: 736482658 Change-Id: I3caeff5abafb69998e56ccd792f5d02e7e04e430 --- sample/compile.cc | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/sample/compile.cc b/sample/compile.cc index 9acc79ee..a5a572d8 100644 --- a/sample/compile.cc +++ b/sample/compile.cc @@ -26,9 +26,9 @@ static constexpr char helpstring[] = "\n Usage: compile infile outfile\n" " infile can be in mjcf, urdf, mjb format\n" - " outfile can be in mjcf, mjb, txt format\n" - " if outfile is missing no output will be generated\n\n" - " Example: compile model.xml model.mjb\n"; + " outfile can be in mjcf, mjb, txt format, or empty\n\n" + " if infile is mjcf, compilation will be timed twice to measure the impact of caching\n\n" + " Example: compile model.xml [model.mjb]\n"; // timer mjtNum gettm(void) { @@ -136,13 +136,20 @@ int main(int argc, char** argv) { } // load model - double starttime = gettm(); + double first=0, second=0; if (type1==typeXML) { + double starttime = gettm(); m = mj_loadXML(argv[1], 0, error, 1000); + first = 1e-6 * (gettm() - starttime); + if (m) { + mj_deleteModel(m); + starttime = gettm(); + m = mj_loadXML(argv[1], 0, error, 1000); + second = 1e-6 * (gettm() - starttime); + } } else { m = mj_loadModel(argv[1], 0); } - double tottime = 1e-6 * (gettm() - starttime); // total time, in seconds // check error if (!m) { @@ -166,6 +173,14 @@ int main(int argc, char** argv) { // finalize char msg[1000]; - snprintf(msg, sizeof(msg), "Done.\nCompiled in %.3g seconds\n", tottime); + if (first) { + snprintf(msg, sizeof(msg), "Done.\n" + "First compile: %.4gs\n" + "Second compile: %.4gs", + first, second); + } else { + snprintf(msg, sizeof(msg), "Done."); + } + return finish(msg, m); }