local gettimeofday = os.gettimeofday local record TopicTimerRecord subtopics: {string: integer} collect: function(): integer start: function(): integer stop: function(integer) stop_subtopic: function(integer, string) wrap: function(function()) wrap_subtopic: function(function(): string) end local topics: {string: TopicTimerRecord} = setmetatable({}, {__index = function(t: {string: TopicTimerRecord}, topic: string): TopicTimerRecord local total_time = 0 local subtopics: {string: integer} = {} local function record_and_return(t0: integer, ...: any): any... total_time = total_time + (gettimeofday() - t0) return ... end local function record_and_return_subtopic(t0: integer, subtopic: string, ...: any): any... local dt = gettimeofday() - t0 subtopics[subtopic] = (subtopics[subtopic]) + dt total_time = total_time + dt return ... end local result: TopicTimerRecord = { subtopics = subtopics, collect = function(): integer return total_time end, start = gettimeofday, stop = function(t0: integer) total_time = total_time + (gettimeofday() - t0) end, stop_subtopic = function(t0: integer, subtopic: string) local dt = gettimeofday() - t0 subtopics[subtopic] = (subtopics[subtopic]) + dt total_time = total_time + (gettimeofday() - t0) end, wrap = function(f: function(...: any): any...): function(...: any): any... return function(...: any): any... return record_and_return(gettimeofday(), f(...)) end end, wrap_subtopic = function(f: (function(...: any): string, any...)): function(...: any): any... return function(...: any): any... return record_and_return_subtopic(gettimeofday(), f(...)) end end, } t[topic] = result return result end}) --[[ luatexbase.add_to_callback('finish_pdffile', function() -- wrapup_run would be better but overwrites useful built-in functionality. local all_topics = {} for topic in pairs(topics) do all_topics[#all_topics + 1] = topic end table.sort(all_topics) texio.write_nl("Timings:") for _, topic in ipairs(all_topics) do texio.write_nl(string.format(" %s:\t%fs", topic, topics[topic].collect())) end end, 'luatagging.timing') ]] return topics