Random numbers: which should i be using?

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 4888 Views - Last Post: 31 December 2012 - 02:19 PM Rate Topic: -----

#16 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

Re: Random numbers: which should i be using?

Posted 30 December 2012 - 02:31 PM

View PostSkydiver, on 30 December 2012 - 01:59 PM, said:

As per MSDN:

Quote

System.Environment.TickCount is the number of milliseconds since the system has started.


Here "system" refers to the operating system. It is not the number of milliseconds since your program has started which seems to be what you are expecting.

If you want numbers that aren't based on the system time, consider using the abstraction offered by the Stopwatch class. http://msdn.microsof....Stopwatch.aspx



No the code I just learnt was the system.enviroment tick because it takes the stop time and minus when I start but it works in example program I use it in mine same way and doesn't work?
Was This Post Helpful? 0
  • +
  • -

#17 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: Random numbers: which should i be using?

Posted 30 December 2012 - 02:37 PM

It is always printing the same difference because the delta between starttime and stoptime is the same, but the actual values for the two variables are different. The delta should always be the same because you are doing a set of operations that take consistent time. I was trying to explain to you why the values for variables are different because you asked:

Quote

now i put a break in on line 10 to check my startt and stop times are different which end up being the same times, even though if i hover mouse over the tickcount in line 7 and then in line 9 both are different

any ideas why they are different but come up as same when i run program?


For example if starttime is 10 and stoptime is 12, the delta would be the same as starttime is 12 and stoptime is 14.
Was This Post Helpful? 0
  • +
  • -

#18 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

Re: Random numbers: which should i be using?

Posted 31 December 2012 - 02:26 AM

well when i debug the code and out a break just after the two lines , i get this

 int[] score = ReadIntArray();
        ReverseInputArray r = new ReverseInputArray(score);
        r.Display("The original array is ");
        ReverseInputArray s = new ReverseInputArray(score);
        int starttime = System.Environment.TickCount; ( for example 8145096857)
        s.Reverse(0, score.Length - 1);
        int stop = System.Environment.TickCount; ( for example 8145097057) [b]<<<<<< This is showing that they are different but then the actual "int stop" shows same value as int starttime??????[/b]
        int totaltime = stop - starttime;
        Console.WriteLine(" the time used in milliseconds is {0}", stop - starttime);
        r.Display("The reversed array is ");
        Console.ReadLine();



i changed the ints to long seeing as i needed a higher max value but still dont help?
Was This Post Helpful? 0
  • +
  • -

#19 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: Random numbers: which should i be using?

Posted 31 December 2012 - 06:47 AM

Are you sure that the operation you are timing takes more than the clock resolution of your CPU?

Are you sure you are compiling in Debug mode? If you are compiling for Release, the Microsoft compilers (including the JIT compiler) is pretty smart about being efficient about register and memory usage, but it tends to confuse the debugger.

As for me the following code always gives me different values for stop and starttime.
using System;
using System.Data;
using System.Threading;

namespace SimpleCsConsole2012
{

class Program
{
    static void Main()
    {
        int starttime = System.Environment.TickCount;
        Thread.Sleep(500);
        int stop = System.Environment.TickCount;
        int totaltime = stop - starttime;
        Console.WriteLine("starttime={0}, stop={1}", starttime, stop);
        Console.WriteLine("The time used in milliseconds is {0}", stop - starttime);
        Console.ReadLine();
    }
}

}


Was This Post Helpful? 0
  • +
  • -

#20 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

Re: Random numbers: which should i be using?

Posted 31 December 2012 - 09:45 AM

im guessing the only difference being the sleep ? making ur numbers be 500 apart?

my cpu speed is 2800, so your saying if the loop im trying to time doesnt take longer than 2800 then it wont register?
Was This Post Helpful? 0
  • +
  • -

#21 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Random numbers: which should i be using?

Posted 31 December 2012 - 02:19 PM

Since the tick count has a resolution of 1 millisecond (1 thousandth of a second), if your operations take less than that, you won't see a difference. Modern computers clocks run in the billion(s) so if you are using 1 cycle instructions, you can execute a million of them per TickCount.

The Stopwatch class (as mentioned before) will give you a higher resolution. The Stopwatch.Frequency field gives the number of timer ticks (not to be confused with TickCount) per second. On my system that is 2,929,765 so it has a resolution of 0.0000003413 seconds, or about 341 nanoseconds (billionths of a second). That gives ~2930 Stopwatch ticks per TickCount.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2