I need to bulid an application, that runs under 1 sec, but I cant find out how to determine the running time.
5 Answers
using System;
using System.Diagnostics;
//Setting a Stopwatch
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < length; i++)
{
//some logic there
}
//Stopping a Stopwatch
sw.Stop();
Console.WriteLine(sw.Elapsed);
//delay
Console.ReadLine();
This is a simple example of using Stopwatch. But don't forget to write "using System.Diagnostics;", otherwise Stopwatch just won't be founded.

- 71
- 1
- 2
-
Welcome to Stack Overflow Dmitriy Yermolenko. Please explain what your answer does so that the author of the question can understand it. For more information please refer to ["How do I write a good answer?"](https://stackoverflow.com/help/how-to-answer) and the [Help Center](https://stackoverflow.com/help) – GGG Aug 27 '18 at 22:29
What about using the Stopwatch class.
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx
Particularly notice the note called out on MSDN
On a multiprocessor computer, it does not matter which processor the thread runs on. However, because of bugs in the BIOS or the Hardware Abstraction Layer (HAL), you can get different timing results on different processors. To specify processor affinity for a thread, use the ProcessThread.ProcessorAffinity method.

- 6,402
- 1
- 32
- 39
You can also get the time at the beginning and at the end of the program, and substract it
http://msdn.microsoft.com/en-us/library/system.datetime.now.aspx

- 40,203
- 9
- 86
- 108
-
1Probably better to use DateTime.UtcNow in the (unlikely) event that you happen to be measuring when the clocks change. – spender Apr 06 '11 at 23:03
-