Reputation: 289
I know this question has been asked more than once, but I'm not sure if the results I'm having are right. The operation seems too fast, so I'd like to double check if that's really it.
I have a routine that splits a string into a List<byte[]>
. I wanted to check the time it takes for the operation, so I modified the code to be like the following:
// Deserializes base64 received from POST service
var str = JsonConvert.DeserializeObject<JsonText>(body).text;
Stopwatch stopWatch = Stopwatch.StartNew();
// parseText is a routine that splits str into
// byte[] of maximum size 100 and puts them into
// a List<byte[]> that is then returned
commands = DummyClass.parseText(str);
stopWatch.Stop();
TimeSpan timespan = stopWatch.Elapsed;
Console.WriteLine(timespan.TotalMilliseconds.ToString("0.0###"));
...
I ran the routine using a 8000 character string and expected a couple miliseconds op time, but surprisingly the whole operation runs to at most 0.8ms which I expected to be a whole lot slower.
Am I reading the measurements wrong? Does 0.8 means 8ms? Did I do something wrong while measuring the time?
Thank you very much!
Upvotes: 12
Views: 14861
Reputation: 2016
I think that your operation runs much faster than 1 ms, to measure such small values use timer ticks:
stopWatch.ElapsedTicks
Upvotes: 1
Reputation: 775
Instead of
TimeSpan timespan = stopWatch.Elapsed;
Console.WriteLine(timespan.TotalMilliseconds.ToString("0.0###"));
Why not try
Console.WriteLine("Elapsed time {0} ms",stopWatch.ElapsedMilliseconds);
You want milliseconds, you have them in stopwatch class directly - no need to visit string.format and timespan libraries.
Upvotes: 7