Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a good way of checking the amount of time it took to run some code in C#?

Tags:

c#

datetime

What I mean is...

get the time, run the code, get the time, compare the time and get the seconds out:

am I doing this right?

DateTime timestamp = DateTime.Now;
//...do the code...
DateTime endstamp = DateTime.Now;

string results = ((endstamp.ticks - timestamp.ticks)/10000000).ToString();
like image 524
MetaGuru Avatar asked Sep 07 '10 20:09

MetaGuru


People also ask

How do you time how long a function takes in C?

Using clock() function We can use the clock() function provided by the <time. h> header file to calculate the CPU time consumed by a task within a C application. It returns the clock_t type, which stores the total number of clock ticks.

How do you measure running time of a program?

To calculate the running time, find the maximum number of nested loops that go through a significant portion of the input. Some algorithms use nested loops where the outer loop goes through an input n while the inner loop goes through a different input m. The time complexity in such cases is O(nm).

What is the measure of time taken to get the output?

Hence the correct answer is Response time. Throughput: It is nothing but the measure of work i.e., the number of processes that are completed per time unit.


3 Answers

You should use Stopwatch for this, for example:

var sw = Stopwatch.StartNew();
//...do the code...
sw.Stop();
var result = sw.ElapsedTicks; //ticks it took
//or less accurate/for bigger tasks, sw.ElapsedMilliseconds

Edited to include @Brian's improvement from comments.

like image 93
Nick Craver Avatar answered Sep 21 '22 03:09

Nick Craver


As many people have noted, the high-precision Stopwatch class is designed for answering the question "how long did this take?" whereas the DateTime class is designed for answering the question "when does Doctor Who start?" Use the right tool for the job.

However, there is more to the problem of correctly measuring elapsed time than simply getting the timer right. You've also got to make sure that you're measuring what you really want to measure. For example, consider:

// start the timer
M();
// stop the timer
// start another timer
M();
// stop the timer

Is there going to be a significant difference between the timings of the two calls? Possibly yes. Remember, the first time a method is called the jitter has to compile it from IL into machine code. That takes time. The first call to a method can be in some cases many times longer than every subsequent call put together.

So which measurement is "right"? The first measurement? The second? An average of them? It depends on what you are trying to optimize for. If you are optimizing for fast startup then you care very very much about the jit time. If you are optimizing for number of identical pages served per second on a warmed-up server then you don't care at all about jit time and should be designing your tests to not measure it. Make sure you are measuring the thing you are actually optimizing for.

like image 44
Eric Lippert Avatar answered Sep 24 '22 03:09

Eric Lippert


No. Use the System.Diagnostics.Stopwatch class instead. DateTime.Now doesn't have the level of precision that you desire (although the DateTime struct is plenty precise, in and of itself).

Stopwatch watch = new Stopwatch();
watch.Start();
// do stuff
watch.Stop();
long ticks = watch.ElapsedTicks;
like image 41
Anthony Pegram Avatar answered Sep 22 '22 03:09

Anthony Pegram