Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
I want to check the amount of time it takes for a specific function to be performed ,so that I will add accurate time delay on my program...
I tried to write this before and after my function:
C#
public static String GetTimestamp(this DateTime value)
      {
          return value.ToString(mmssffff);
      }


Then I calculated the difference between both results, and I get 350.I dont know if it microseconds or milliseconds...

Do you know the meaning of the result?(350)
Do you have another idea to do this?
Thanks!
Posted
Updated 23-Nov-13 11:37am
v3

Why don't you write something like this:

C#
DateTime start = DateTime.Now; //Start time
DoFoo(); //Execute your method
DateTime end = DateTime.Now;   //End time

TimeSpan timeDifference = end - start; //Time span between start and end = the time span needed to execute your method
int difference_Miliseconds = (int)span.TotalMilliseconds; //Gives you the time needed to execute the method in miliseconds (= time between start and end in miliseconds)
 
Share this answer
 
Comments
Member 10304617 23-Nov-13 16:30pm    
You are right, its better!
Thanks!!!
Marco Bertschi 23-Nov-13 17:35pm    
You are welcome, glad that I was Abel to help you.
Sergey Alexandrovich Kryukov 23-Nov-13 20:33pm    
It will work, but this isn't a most accurate method. Please see my solution.
—SA
Marco Bertschi 24-Nov-13 5:59am    
Hi Sergey,

I know that my solution is not the most exact one. Thank you for providing another, more exact solution.
Sergey Alexandrovich Kryukov 24-Nov-13 10:18am    
My pleasure.
—SA
The method shown in Solution 1 is not the most accurate. The most accurate timing is provided by the class System.Diagnostics.Stopwatch:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch%28v=vs.110%29.aspx[^].

You can find out time resolution provided by checking these two static fields:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.frequency(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.ishighresolution(v=vs.110).aspx[^].

—SA
 
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