Now takes the total time estimated at the top if totalTime is not provided.
[profiler-torch.git] / profiler.lua
index 4420af7..4fec258 100644 (file)
@@ -94,14 +94,20 @@ 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
+   local s
+
+   s = string.format('%s %.02fs %s[%.02f%%]',
+                     l, t,
+                     profiler.colors('blue'),
+                     100 * t / totalTime
+   )
+
    if nbSamples then
       s = s .. string.format(profiler.colors('green') .. ' (%.01fmus/sample)', 1e6 * t / nbSamples)
    end
+
    s = s .. profiler.colors('black')
+
    return s
 end
 
@@ -114,6 +120,8 @@ function profiler.print(model, nbSamples, totalTime, indent)
       localTotal = localTotal + t
    end
 
+   totalTime = totalTime or localTotal
+
    if torch.isTypeOf(model, nn.Container) then
       hint = ' '
    else
@@ -125,10 +133,11 @@ function profiler.print(model, nbSamples, totalTime, indent)
       hint = hint .. profiler.colors('red')
    end
 
-   print(profiler.timing(indent .. hint .. ' ' .. model.__typename, localTotal, nbSamples, totalTime))
+   print(profiler.timing(indent .. hint .. ' ' .. model.__typename,
+                         localTotal, nbSamples, totalTime))
 
    for l, t in pairs(model.accTime) do
-      print(profiler.timing(indent .. '  ' .. l, t, nbSamples, totalTime))
+      print(profiler.timing(indent .. '  :' .. l, t, nbSamples, totalTime))
    end
 
    print()