6 Replies - 5530 Views - Last Post: 21 February 2010 - 12:02 PM Rate Topic: -----

#1 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Timing programs with Datetime.Now.Ticks

Posted 20 February 2010 - 02:02 PM

I'm supposed to time 100000 iterations of binary search in C#, using different array sizes. But when I change the array sizes I get the same number or two times said number. I'm not terribly familiar with C# so it might be something different from C++ that I'm not aware of.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Q5CS
{
    class Program
    {
      
            
            /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

    public static int binarySearch(int[] array, int value, int left, int right) {
     while(left<=right)
     {
    int middle = (left + right) / 2;
    //System.out.println(middle);
            if (array[middle] == value)

                  return middle;

            else if (array[middle] > value)

                  right = middle - 1;

            else

                  left = middle + 1;
     }//end while
      return -1;
     }//end binary search
    /**
     * @param args the command line arguments
     */

    public static void Main(String[] args) {

int right=128*128-1;
int i=0;
int left=0;
int value=1;
int []a;
int ans;
long time = 0, newTime = 0;
a= new int[right+1];

 
        for(i=0;i<=right;i++)
    {
    a[i]=2*i;
    }//end for
Console.WriteLine(a[right] + " " + right);
Console.ReadLine();
time = DateTime.Now.Ticks;
for(i=0;i<100000;i++)
    {
    ans=binarySearch(a,value,left,right);
    }//end for
newTime = DateTime.Now.Ticks;
Console.WriteLine("The program takes ");
Console.WriteLine((time) * 100);
Console.WriteLine((newTime) * 100);
Console.WriteLine((newTime-time)*100);
Console.WriteLine(" nanoseconds");
Console.ReadLine();
        // TODO code application logic here




        }
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Timing programs with Datetime.Now.Ticks

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Timing programs with Datetime.Now.Ticks

Posted 20 February 2010 - 02:37 PM

The resolution of the DateTime.Now property is 10 milliseconds. If your for() loop takes enough less than that between 2 calls to Now, you will read the same time value in ticks.


See DateTime.Now Property
Was This Post Helpful? 1
  • +
  • -

#3 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Timing programs with Datetime.Now.Ticks

Posted 20 February 2010 - 03:05 PM

View Postn8wxs, on 20 February 2010 - 01:37 PM, said:

The resolution of the DateTime.Now property is 10 milliseconds. If your for() loop takes enough less than that between 2 calls to Now, you will read the same time value in ticks.


See DateTime.Now Property


I'm getting like a 8 digit number, and it's not 0. I think it was like 15625000 and 31250000. One is double the other. Again, this is depending on the number I choose for the size of the array. I'm wondering if it has to do with data types or something but I'm using longs. That should be accurate enough right?
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Timing programs with Datetime.Now.Ticks

Posted 20 February 2010 - 04:40 PM

Does your code take approximately 1.5 seconds to execute?

The tick interval is 100 nanoseconds - 10e-7. Divide the difference in ticks between the two tick counts by 10,000,000 to get seconds.

15625000 / 10000000 = 1.5625 seconds.
Was This Post Helpful? 1
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,411
  • Joined: 29-May 08

Re: Timing programs with Datetime.Now.Ticks

Posted 20 February 2010 - 06:47 PM

Or look at the Diagnostic.StopWatch class which has 1ns accuracy.
Was This Post Helpful? 1
  • +
  • -

#6 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Timing programs with Datetime.Now.Ticks

Posted 20 February 2010 - 09:23 PM

View Postn8wxs, on 20 February 2010 - 03:40 PM, said:

Does your code take approximately 1.5 seconds to execute?

The tick interval is 100 nanoseconds - 10e-7. Divide the difference in ticks between the two tick counts by 10,000,000 to get seconds.

15625000 / 10000000 = 1.5625 seconds.


I multiplied my Datetime numbers by 100 to get 10e-9 , so my program takes .15 milliseconds.

I don't know if the problem is the Datetime function (or the way I use it), like I was saying. The problem is that when I increase the size of the array, it says the time stays the same. If I increase the size of the array enough it just doubles.

Maybe I'll try Diagnostic.StopWatch and see if that works. Is it easy to implement?
Was This Post Helpful? 0
  • +
  • -

#7 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Timing programs with Datetime.Now.Ticks

Posted 21 February 2010 - 12:02 PM

Using stopwatch seems to have worked.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1