 |
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sun Jan 06, 2008 2:08 pm Post subject: [C#] Something's Fishy Here... |
|
|
How long do you think it takes a computer to assign a value (0) to a variable (an element in an array)?
According to a test I've made, it takes exactly no time at all.
Does that not sound a little weird?
We're talking about fractions of a second, of course, but my timer measures that.
~~
Here is my output for the loop (note that l = already defined as a long)
Code: |
for (l = 0; l < 1000000; l++)
{ }
|
Code: |
Starting Time: 1/6/2008 1:46:44 PM
Ending Time: 1/6/2008 1:46:44 PM
Time Elapsed: 00:00:00.0156250
|
There's nothing wrong with that, but here's the next loop:
Code: |
for (l = 0; l < 999999; l++) //simply because we'll change the last one, to stop the Array.Find()
{
longs[l] = 0;
}
longs[999999] = 1;
|
And here's a simplified version of the loop (this is initializing):
Code: |
Starting Time: 1/6/2008 1:48:40 PM
Ending Time: 1/6/2008 1:48:40 PM
Time Elapsed: 00:00:00.0156250
|
Wtf, right?
Here's my complete code--I was testing the speed difference between a for-loop and an Array.Find<T>() method:
Code: |
static void Main(string[] args)
{
//create all the vars
//for loop
DateTime for_start;
DateTime for_end;
TimeSpan for_time;
//Array.Find() loop
long l;
long[] longs = new long[1000000];
DateTime ini_start; //init
DateTime ini_end;
TimeSpan ini_time;
DateTime fin_start; //find start
DateTime fin_end;
TimeSpan fin_time;
TimeSpan arr_time; //whole Array.Find() time
Console.WriteLine("Getting ready to start \"for\" loop test.");
Console.WriteLine("Press enter to begin."); Console.ReadLine();
for_start = DateTime.Now;
for (l = 0; l < 1000000; l++)
{ }
for_end = DateTime.Now;
for_time = for_end.Subtract(for_start);
Console.WriteLine("Starting time: " + for_start.ToString());
Console.WriteLine("Ending time: " + for_end.ToString());
Console.WriteLine("Time elapsed: " + for_time.ToString());
Console.ReadLine();
Console.Clear();
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Console.WriteLine("Getting ready to start \"Array.Find\" loop test.");
Console.WriteLine("Press enter to begin."); Console.ReadLine();
//start init
ini_start = DateTime.Now;
for (l = 0; l < 999999; l++) //simply because we'll change the last one, to stop the Array.Find()
{
longs[l] = 0;
}
longs[999999] = 1;
ini_end = DateTime.Now;
ini_time = ini_end.Subtract(ini_start);
fin_start = DateTime.Now; //so the previous calculation doesn't slow us down
Array.Find<long>(longs, MyPred);
fin_end = DateTime.Now;
fin_time = fin_end.Subtract(fin_start);
arr_time = ini_time.Add(fin_time);
Console.WriteLine("Initialization time start: " + ini_start.ToString());
Console.WriteLine("Initialization time end: " + ini_end.ToString());
Console.WriteLine("Initialization time total: " + ini_time.ToString());
Console.WriteLine();
Console.WriteLine("Array.Find() time start: " + fin_start.ToString());
Console.WriteLine("Array.Find() time end: " + fin_end.ToString());
Console.WriteLine("Array.Find() time total: " + fin_time.ToString());
Console.WriteLine();
Console.WriteLine("Total Array.Find() (Initializing and Finding) time: " + arr_time.ToString());
Console.ReadLine();
Console.Clear();
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Console.WriteLine("Now for comparison: ");
Console.WriteLine();
Console.WriteLine("\"for\" loop total time: " + for_time.ToString());
Console.WriteLine("Array.Find() total time: " + arr_time.ToString());
Console.ReadLine();
}
private static bool MyPred(long l)
{
return (l == 1);
}
|
I'm guessing that there's something wrong with my code--I know that it takes like no time at all to assign a value to an element in an array, but seriously, it can't be that fast: it has to first figure out what "l" is, then it has to figure out where that element of the array is stored, then it has to stick something in there. There is no freaking way that it takes the same ammount as doing nothing.
_________________
|
|
Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sun Jan 06, 2008 3:27 pm Post subject: |
|
|
I know it won't take a second. The point is, however, that in a million assignments and what not, it took this long:
Exactly the same as it took to do nothing.
It didn't even take a 10 millionth of a second to do a million assignments and whatever. Does that not strike you as odd?
_________________
|
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|