From: Francois Fleuret Date: Tue, 6 Dec 2016 08:38:04 +0000 (+0100) Subject: Added color! X-Git-Url: https://www.fleuret.org/cgi-bin/gitweb/gitweb.cgi?p=profiler-torch.git;a=commitdiff_plain;h=78ce9a217ef066c8f01b1531c27c6ef38c45ece1 Added color! --- diff --git a/README.md b/README.md index 2adb72c..0e851f4 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,10 @@ This is a simple profiler to estimate processing time per module per function. It seems to work okay, but there was no heavy testing so far. See test-profiler.lua for a short example. +### profiler.color ### + +This is a Boolean flag to state if the printing should be done in color. It is true by default. + ### profiler.decorate(model, [functionsToDecorate]) ### This function should be called before starting the computation. @@ -14,3 +18,5 @@ It also resets the accumulated timings to zero. ### profiler.print(model, [nbSamples], [totalTime]) ### Prints the measured processing times. If nbSamples is provided, the time per samples will also be printed. If totalTime is provided, the percentages will also be printed. + +Non-Containers are hilighted with a '*' or in red. diff --git a/profiler.lua b/profiler.lua index 3bbccaf..77398a0 100644 --- a/profiler.lua +++ b/profiler.lua @@ -37,6 +37,16 @@ require 'sys' profiler = {} +profiler.color = true + +profiler.colors = function(name) + if profiler.color then + return sys.COLORS[name] + else + return '' + end +end + function profiler.decorate(model, functionsToDecorate) local functionsToDecorate = functionsToDecorate or @@ -80,27 +90,42 @@ function profiler.decorate(model, functionsToDecorate) end +function profiler.timing(l, t, nbSamples, totalTime) + local s = string.format('%s %.02fs', l, t) + if totalTime then + s = s .. string.format(profiler.colors('blue') .. ' [%.02f%%]', 100 * t / totalTime) + end + if nbSamples then + s = s .. string.format(profiler.colors('green') .. ' (%.01fmus/sample)', 1e6 * t / nbSamples) + end + s = s .. profiler.colors('black') + return s +end + function profiler.print(model, nbSamples, totalTime, indent) local indent = indent or '' local hint + local localTotal = 0 + for _, t in pairs(model.accTime) do + localTotal = localTotal + t + end + if torch.isTypeOf(model, nn.Container) then hint = ' ' else - hint = '*' + if profiler.color then + hint = ' ' + else + hint = '*' + end + hint = hint .. profiler.colors('red') end - print(string.format('%s%s %s', indent, hint, model.__typename)) + print(profiler.timing(indent .. hint .. ' ' .. model.__typename, localTotal, nbSamples, totalTime)) for l, t in pairs(model.accTime) do - local s = string.format('%s %s %.02fs', indent, l, t) - if totalTime then - s = s .. string.format(' [%.02f%%]', 100 * t / totalTime) - end - if nbSamples then - s = s .. string.format(' (%.01fmus/sample)', 1e6 * t / nbSamples) - end - print(s) + print(profiler.timing(indent .. ' ' .. l, t, nbSamples, totalTime)) end print() diff --git a/test-profiler.lua b/test-profiler.lua index 18677ec..aa6e800 100755 --- a/test-profiler.lua +++ b/test-profiler.lua @@ -93,9 +93,9 @@ end -- Print the accumulated timings +-- profiler.color = false profiler.print(model, nbSamples, modelTime) -- profiler.print(model) -print() print(string.format('Total model time %.02fs', modelTime)) print(string.format('Total data time %.02fs', dataTime))