Added color!
[profiler-torch.git] / profiler.lua
index 3bbccaf..77398a0 100644 (file)
@@ -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()