2

I need to calculate the time difference faken for division most accurately in nano seconds. Please tell me to do this.

At Present i'm using a lower accuracy method in which the problem is that : when the first calculation is performed it shows 87 milliseconds or 65 milliseconds as answer. But when the function is called again second time or more, it only show 0 milliseconds.

The code is :

long startTick = DateTime.Now.Ticks;
double result = (double)22 / 7;
result = System.Math.Round(result, digit);                
long endTick = DateTime.Now.Ticks; 
long tick = endTick - startTick;
double milliseconds = tick / TimeSpan.TicksPerMillisecond;
time.Text = result + "\nThe division took " + milliseconds + " milliseconds to complete.";

digit is the parameter of function which is variable. No matter what the value of digit is the milliseconds value remains 0 after first calling of function....

Please suggest more accurate way in which calling the same function with different decimal digits will result in different time interval in c# for windows Phone.

I think the memory flush should be done before and after each calculation. But i dont know how to do this.

I don't like this tick method personally for accuracy. I've tried stopwatch also but its not working. Please suggest another method best suited in my case. I want result like : 0.0345 or 0.0714 seconds.

Thanks

Sunil Kumar
  • 622
  • 1
  • 12
  • 33
  • 1
    You want nanosecond precision on a *phone*? Or did you mean milliseconds? – vcsjones Dec 13 '11 at 21:12
  • Whatever maximum precision i can get... in milli or micro or nano.. – Sunil Kumar Dec 13 '11 at 21:14
  • Ticks are not ticking fast enough to catch a single division + round. Try copying the same pair of lines, say, 1000 times (do not put it in a loop, otherwise CPU cycles spent on loop instructions would pollute your result) and see if you get a few ticks. Then divide the result by 1000 - it will be your answer. – Sergey Kalinichenko Dec 13 '11 at 21:14
  • Hey @dasblinkenlight thanks... I got u... got my answer – Sunil Kumar Dec 13 '11 at 21:38

4 Answers4

4

You are performing integer division on this line:

double milliseconds = tick / TimeSpan.TicksPerMillisecond;

Even though you are declaring it as a double, a long divided by a long will truncate the decimal. You are better off doing:

double milliseconds = (double)tick / TimeSpan.TicksPerMillisecond;

Or better yet, just ditch the tick stuff all together:

DateTime start = DateTime.Now;
double result = (double)22 / 7;
result = System.Math.Round(result, digit);                
DateTime end = DateTime.Now;
double milliseconds = (end - start).TotalMilliseconds;
time.Text = result + "\nThe division took " + milliseconds + " milliseconds to complete.";

You won't be able to get micro or nano level precision, but you will get millisecond precision with a margin of error.

You still may get zero, however. You are trying to time how long a simple division operation takes. You could do millions of division operations in less than a second. You may want to do it 1,000,000 times, then divide the result by a 1,000,000:

DateTime start = DateTime.Now;
for (var i = 0; i < 1000000; i++)
{
    double result = (double)22 / 7;
    result = System.Math.Round(result, digit);
}
DateTime end = DateTime.Now;
double milliseconds = (end - start).TotalMilliseconds / 1000000;

This still won't be completely realistic, but should get you an actual number.

vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • Its having the same problem ie milliseconds=0 is answer after first time calling. Not working after first division. – Sunil Kumar Dec 13 '11 at 21:22
  • Hey Jones Thanks for the help. I understood the concept and the edited code worked. Got answer in right format. Tell me i can do this on mobile device without any problem...??? – Sunil Kumar Dec 13 '11 at 21:37
  • @SunilKumar It should work; but be aware that it won't be scientifically accurate, but accurate enough. – vcsjones Dec 13 '11 at 22:22
  • Can you please help me on this http://stackoverflow.com/questions/22632006/timer-start-time-and-end-time-calculation-gives-4-secs-extra – user2056563 Mar 27 '14 at 10:36
0

Since you have the time in ticks, just increase the resolution by multiplying the denominator:

double microseconds = tick / (TimeSpan.TicksPerMillisecond * 1000.0);
Tudor
  • 61,523
  • 12
  • 102
  • 142
0

Why are you not using StopWatch Class to do your time calulation. It is meant to the calculate the time the you want .. Here is a link for your reference.

http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx

0

//if you want to get the full milliseconds you could also do something like this.

dateStartTime = Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString());
//then where you end the code do this
dateEndTime = Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString());
ddateDuration = (TimeSpan)(dateEndTime - dateStartTime);
then to display out what you are actually looking for in terms of miliseconds do
Console.WriteLine(ddateDuration.ToString().Substring(0, 8)); 
// or some other method that you are using to display the results
MethodMan
  • 18,625
  • 6
  • 34
  • 52