Here some lua code for timing performance of code. The socket library is build-in with the LuaTeX compiler, ensure is enabled with the --socket option. As you will see LaTeX programs run very fast. They are slowed down by loading too many large fonts, large images and tikz graphics.
\begin{filecontents*}[overwrite]{mytimer.lua}
-- table to hold time performance
M = M or {}
local socket = require('socket')
local time_stat = time_stat or {}
local gettime = socket.gettime
-- start time measurement
time_stat["total"] = 0
-- @param done indicates timer was stopped
function start_time_measure(field)
time_stat[field] = {start = gettime(), done = false}
end
-- stop time measurement
-- if two calls to stop will return wrong value
function stop_time_measure(field)
if not time_stat[field].done then
time_stat[field] = {total = gettime() - time_stat[field].start, done=true}
end
end
M.start = start_time_measure
M.stop = stop_time_measure
M.statistics = function(name) return time_stat[name].total end
-- M.start("document")
-- local s = 0
-- for i=1, 1 do s=s+1 end
-- M.stop("document")
-- print(M.statistics("document"))
-- print(M.statistics("document"))
return M
\end{filecontents*}
\directlua{timer = require("mytimer")}
\def\timerstart#1{\directlua{timer.start("#1")}}
\def\timerstop#1{\directlua{timer.stop("#1")}}
\def\timerprint#1{
\directlua{
timer.stop("#1")
tex.print("elapsed time (#1):", timer.statistics("#1").." s")
}}
\timerstart{document}
\documentclass{article}
\timerstart{package}
\usepackage{fontspec}
\setmainfont{Arial Unicode MS}
\timerstop{package}
\usepackage{lipsum}
\usepackage{luacode}
\usepackage{microtype}
\begin{document}
Hello
\timerprint{package}
\begin{luacode}
z=0
for i=0,150000000,1 do
i=i+1
z=i
end
tex.print(z)
\end{luacode}
\timerstart{lipsum}
\lipsum[1-12]
\timerstop{lipsum}
\timerprint{package}
\timerprint{lipsum}
\timerprint{document}
\end{document}
