Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I using the TimeSpan to count the time from the beginning of my program to the end
I use this code to count the time but when I running my code show time that is different when I run the same code again what is the error?


C#
DateTime  startTime = DateTime.Now;

do somethings

DateTime endTime = DateTime.Now;
TimeSpan totalTimeTaken = endTime - startTime;
Posted
Updated 10-Jun-15 10:03am
v3
Comments
Maciej Los 10-Jun-15 16:06pm    
What?
neveen neveen 10-Jun-15 16:13pm    
I work on algorithm find path between 2 location when I select the start (2,7) and goal (6,3) there is no problem is show the time but again when select the same start (2,7) and goal (6,3) I have different time what is the error
Maciej Los 10-Jun-15 16:15pm    
Based on this piece of code it's impossible to answer this question.
[no name] 10-Jun-15 16:09pm    
The error is that you should be using the Stopwatch class.

Why are you using DateTime.Now to do this??

There is the System.Diagnostics.Stopwatch class that's a lot better for timing something like this.
 
Share this answer
 
Comments
Maciej Los 10-Jun-15 16:40pm    
Yeah!
Here is a link to StopWatch class
Afzaal Ahmad Zeeshan 10-Jun-15 17:42pm    
A very much perfect solution! +5
As Dave and VJ indicated you should use System.Diagnostics.Stopwatch.

Some general guidelines for performance timing:
Pre-compute all of the input values you're going to use and store them in simple structures (e.g. arrays). (So that computation time is excluded.)
Run each of the tests several (many) times and average the results.
Ignore the first time through the tests in order to exclude JIT execution time.
 
Share this answer
 
Check this -

C#
var stopWatch = new Stopwatch();
stopWatch.Start();
//do somthing here
stopWatch.Stop();

 var ts = stopWatch.Elapsed;

// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 10-Jun-15 17:43pm    
I voted 4, but you need to explain it a little more, as to what this answer tells the OP, or how is it helpful. :)
Measuring short time intervals is not trivial. You have to think about what is happening. In your case the execution of your little piece of the code is only part of it. Many other things are going on and these are different for C# code running in a .Net environment to say an assembly language console app.

Before you measure anything you need to understand where the errors may be (they are always there) and think about how to minimise their effect.

The other answers cover most of it but I would suggest you look at:

http://stackoverflow.com/questions/969290/exact-time-measurement-for-performance-testing[^]

http://stackoverflow.com/questions/1047218/benchmarking-small-code-samples-in-c-can-this-implementation-be-improved[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900