fguihen Posted April 16, 2005 Posted April 16, 2005 (edited) im trying to time how much processing a method of my application does. as soon as the method executes , i have a time span : this.processingStart = System.DateTime.Now;// start of processing duration i have the same at the end of the method. i simply take the start time from the finish time, and i should get the total time taken , but i always get 0. i have a line like: TimeSpan timetaken = processingFinish.Subtract(processingStart); int time = timeTaken.InMiliseconds(); is there something up with timespans that cause this, or can anyone tell me a reason why this is happening? Edited April 16, 2005 by fguihen Quote
HJB417 Posted April 16, 2005 Posted April 16, 2005 TimeSpan timeTake = DateTime.Now.Subtract(processingStart); int time = Convert.ToInt32(timeTake.TotalMilliseconds); - or - TimeSpan timeTake = DateTime.Now - processingStart; int time = Convert.ToInt32(timeTake.TotalMilliseconds); should do the trick, I don't know when processingFinish is set, or what type it is, so I'm assuming it's of type DateTime. Quote
Wile Posted April 16, 2005 Posted April 16, 2005 If you use the system time, make sure you dont run anything else. For instance, running the task manager will regularly give you several milliseconds extra time before your routine is finished. This is because your application is preempted by the OS and the CPU is given to the taskmanager. I don't know if you can somehow get the processing time of an application (like the taskamanager can show) but that would be a safer value to use as it isnt influence by other applications or the OS that do stuff. Quote Nothing is as illusive as 'the last bug'.
Diesel Posted April 16, 2005 Posted April 16, 2005 Scratch that. Making an accurate timer is more complex than just getting the System Time. In .Net, one of the most accurate ways to create a timer is by using QueryPerformanceFrequency and QueryPerformanceCounter, although I've heard that PerformanceFrequency can be inaccurate. http://www.codeproject.com/vb/net/vbnetstopwatch.asp#xx867040xx Quote
IngisKahn Posted April 17, 2005 Posted April 17, 2005 Thankfully in 2.0 they have the StopWatch class. Quote "Who is John Galt?"
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.