9

I need to bulid an application, that runs under 1 sec, but I cant find out how to determine the running time.

Mikee
  • 323
  • 1
  • 3
  • 7
  • 4
    Say "one mississippi". It's too slow if you have to turn the last i into an eeee. Otherwise the question is far too vague. It isn't the same if you run it the first time vs the second time. – Hans Passant Apr 06 '11 at 22:42

5 Answers5

11

Set up a Stopwatch at initialisation and check its Elapsed property at exit.

spender
  • 117,338
  • 33
  • 229
  • 351
7
        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.

  • 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
3

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.

Brad Cunningham
  • 6,402
  • 1
  • 32
  • 39
1

console application does not close if you write
Console.ReadLine();

Hassan Abbas
  • 467
  • 2
  • 8
  • 28
0

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

Aleadam
  • 40,203
  • 9
  • 86
  • 108