Pedro Costa
Pedro Costa

Reputation: 2996

Best way to represent time duration in a log entry

I'm writing a simple generic logger class, that is aware of time started and ended of a task that is being logged.

In English how would you rather see represented the time taken for the task to finish in various time figures?

I currently have,

Log("Finished in {0}ms, {1}s, {2}m.", ts.TotalMilliseconds, ts.TotalSeconds, ts.TotalMinutes);

Which outputs,

2012-02-16@20:12:33: Finished in 2007.8125ms, 2.0078125s, 0.0334635416666667m.

Any better ways you may suggest?

Cheers, P.

Thanks Xeno, final code is:

Log(string.Concat("Finished in ", (ts.Hours > 0 ? ts.Hours + "h " : string.Empty),
                                    (ts.Minutes > 0 ? ts.Minutes + "m " : string.Empty),
                                    (ts.Seconds > 0 ? ts.Seconds + "s " : string.Empty),
                                    (ts.Milliseconds > 0 ? ts.Milliseconds + "ms " : string.Empty),
                                    ("(" + ts.TotalMilliseconds.ToString("0.000") + ")")));

Upvotes: 1

Views: 1596

Answers (5)

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57650

My personal preference is using some standard. Because they are standard and easy to parse later.

For example, HTTP/1.1 Date/Time format which yields any of the following format.

  Sun, 06 Nov 1994 08:49:37 GMT  ; RFC 822, updated by RFC 1123
  Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
  Sun Nov  6 08:49:37 1994       ; ANSI C's asctime() format

Along with this Its better to store the Unix Timestamp. Its always comes handy.

Lastly Put the time duration in miliseconds. If you have miliseconds you can convert it to any other higher unit. However if you want someone else will read it put it in HH:MM:SS.fff or similar format. This format is easier to understand.

Upvotes: 2

Ed Power
Ed Power

Reputation: 8531

If it's going to vary between milliseconds and hours, then I'd stick with a consistent yet familiar notation:

HH:mm:ss.fff

Upvotes: 0

Abdul Hfuda
Abdul Hfuda

Reputation: 1503

How about:

0h 0m 2s 7.815ms

Since its a generic Logger, this was generic suggestion

The best way would be to give the user a way to define this in a configuration file or at least programmatically at initialization time

Upvotes: 3

ardnew
ardnew

Reputation: 2086

You could also simply log the begin and end times to their highest precision, which is probably the most flexible or accurate representation.

Upvotes: 1

Jonathan M
Jonathan M

Reputation: 17451

It all depends on context. If you have a process that typically runs for several minutes, milliseconds is irrelevant. If you have something that runs in a few seconds, milliseconds may be relevant. If it's under 1 second, definitely millis or micros.

Upvotes: 0

Related Questions