View Single Post
Old 02-12-2007, 01:18 PM   #11 (permalink)
Jeff
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Age: 28
Posts: 4,886
Default

Considering the 1st part of your thread here, the int vs Int32. What I find weird is they are treated the same when you output their Type...

Take the following code

Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace SpeedTesting
{
	class Program
	{
		static Stopwatch _stopWatch;
		static void Main(string[] args)
		{
			_stopWatch = new Stopwatch();

			for( int b = 0; b < 15; b++ )
			{
				_stopWatch.Start();
				for( int i = 0; i < int.MaxValue; i++ )
				{
					int a = 0;
				}

				_stopWatch.Stop();

				Output(typeof(int), int.MaxValue, _stopWatch.ElapsedMilliseconds);

				_stopWatch.Reset();

				_stopWatch.Start();
				for( int i = 0; i < int.MaxValue; i++ )
				{
					Int32 a = 0;
				}
		
				_stopWatch.Stop();

				Output(typeof(Int32), int.MaxValue, _stopWatch.ElapsedMilliseconds);

				_stopWatch.Reset();
				Console.WriteLine();
			}

			Console.ReadLine();
		}

		private static void Output(Type type, int times, long milliSeconds)
		{
			Console.WriteLine("Called ({0}) {1} times in {2} milliseconds", type, times, milliSeconds);
		}
	}
}
You will notice that my output of the variable is by Type.

However, here is the output of the application...

Code:
Called (System.Int32) 2147483647 times in 6729 milliseconds
Called (System.Int32) 2147483647 times in 6627 milliseconds

Called (System.Int32) 2147483647 times in 6691 milliseconds
Called (System.Int32) 2147483647 times in 6666 milliseconds

Called (System.Int32) 2147483647 times in 6715 milliseconds
Called (System.Int32) 2147483647 times in 6665 milliseconds

Called (System.Int32) 2147483647 times in 6668 milliseconds
Called (System.Int32) 2147483647 times in 6591 milliseconds

Called (System.Int32) 2147483647 times in 6597 milliseconds
Called (System.Int32) 2147483647 times in 6542 milliseconds

Called (System.Int32) 2147483647 times in 6573 milliseconds
Called (System.Int32) 2147483647 times in 6551 milliseconds

Called (System.Int32) 2147483647 times in 6764 milliseconds
Called (System.Int32) 2147483647 times in 6665 milliseconds

Called (System.Int32) 2147483647 times in 6677 milliseconds
Called (System.Int32) 2147483647 times in 6641 milliseconds

Called (System.Int32) 2147483647 times in 6620 milliseconds
Called (System.Int32) 2147483647 times in 6762 milliseconds

Called (System.Int32) 2147483647 times in 6766 milliseconds
Called (System.Int32) 2147483647 times in 6639 milliseconds

Called (System.Int32) 2147483647 times in 6641 milliseconds
Called (System.Int32) 2147483647 times in 6629 milliseconds

Called (System.Int32) 2147483647 times in 6678 milliseconds
Called (System.Int32) 2147483647 times in 6624 milliseconds
I cut it short cause i didnt wanan run it 15 times nor did i need to after just these few results. You can see that indeed Int32 is slightly faster then the standard int. But still the fact that its refered to as the same Type is really weird.

Now just to make sure there wasnt some odd fluke since i called int first and Int32 second, i reversed the statements as follows

Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace SpeedTesting
{
	class Program
	{
		static Stopwatch _stopWatch;
		static void Main(string[] args)
		{
			_stopWatch = new Stopwatch();

			for( int b = 0; b < 15; b++ )
			{
				_stopWatch.Start();
				for( int i = 0; i < int.MaxValue; i++ )
				{
					Int32 a = 0;
				}

				_stopWatch.Stop();

				Output(typeof(int), int.MaxValue, _stopWatch.ElapsedMilliseconds);

				_stopWatch.Reset();

				_stopWatch.Start();
				for( int i = 0; i < int.MaxValue; i++ )
				{
					int a = 0;
				}
		
				_stopWatch.Stop();

				Output(typeof(Int32), int.MaxValue, _stopWatch.ElapsedMilliseconds);

				_stopWatch.Reset();
				Console.WriteLine();
			}

			Console.ReadLine();
		}

		private static void Output(Type type, int times, long milliSeconds)
		{
			Console.WriteLine("Called ({0}) {1} times in {2} milliseconds", type, times, milliSeconds);
		}
	}
}
and sure enough it was a fluke, the seconds statement is still called faster then the first, but not everytime... Odd eh?

Code:
Called (System.Int32) 2147483647 times in 6768 milliseconds
Called (System.Int32) 2147483647 times in 6658 milliseconds

Called (System.Int32) 2147483647 times in 6660 milliseconds
Called (System.Int32) 2147483647 times in 6648 milliseconds

Called (System.Int32) 2147483647 times in 6765 milliseconds
Called (System.Int32) 2147483647 times in 6651 milliseconds

Called (System.Int32) 2147483647 times in 6679 milliseconds
Called (System.Int32) 2147483647 times in 6672 milliseconds

Called (System.Int32) 2147483647 times in 6686 milliseconds
Called (System.Int32) 2147483647 times in 6693 milliseconds

Called (System.Int32) 2147483647 times in 6622 milliseconds
Called (System.Int32) 2147483647 times in 6659 milliseconds

Called (System.Int32) 2147483647 times in 6763 milliseconds
Called (System.Int32) 2147483647 times in 6632 milliseconds

Called (System.Int32) 2147483647 times in 6565 milliseconds
Called (System.Int32) 2147483647 times in 6649 milliseconds

Called (System.Int32) 2147483647 times in 6687 milliseconds
Called (System.Int32) 2147483647 times in 6736 milliseconds

Called (System.Int32) 2147483647 times in 6629 milliseconds
Called (System.Int32) 2147483647 times in 6598 milliseconds

Called (System.Int32) 2147483647 times in 6677 milliseconds
Called (System.Int32) 2147483647 times in 6745 milliseconds

Called (System.Int32) 2147483647 times in 6617 milliseconds
Called (System.Int32) 2147483647 times in 6594 milliseconds
I find this odd and interesting
__________________
Jeff Boulanger
ConnectUO - Core Developer

Want to help make ConnectUO better? Click here to submit your ideas/requests
Use your talent to compete against other community members in RunUO hosted coding competitions

If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team.


Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO
Jeff is offline   Reply With Quote