Click here to Skip to main content
15,924,452 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# missing anonymous classes? Pin
Judah Gabriel Himango29-Jun-04 9:45
sponsorJudah Gabriel Himango29-Jun-04 9:45 
AnswerRe: C# missing anonymous classes? Pin
Heath Stewart29-Jun-04 9:05
protectorHeath Stewart29-Jun-04 9:05 
Generalmemcmp in C# Pin
BrcKcc29-Jun-04 8:29
BrcKcc29-Jun-04 8:29 
GeneralRe: memcmp in C# Pin
Heath Stewart29-Jun-04 9:45
protectorHeath Stewart29-Jun-04 9:45 
GeneralRe: memcmp in C# Pin
BrcKcc29-Jun-04 9:54
BrcKcc29-Jun-04 9:54 
GeneralRe: memcmp in C# Pin
Werdna30-Jun-04 5:53
Werdna30-Jun-04 5:53 
GeneralRe: memcmp in C# Pin
BrcKcc30-Jun-04 6:24
BrcKcc30-Jun-04 6:24 
GeneralRe: memcmp in C# Pin
Werdna30-Jun-04 6:47
Werdna30-Jun-04 6:47 
Here is a simple program that tests the comparison using safe and unsafe version. On my machine the unsafe version is about 4 times as fast as safe version.
You have to make sure you allow unsafe code in project properties.

Note, this will only work with arrays whose size is divisble by 4. You can easily change it to handle the last 1, 2 or 3 bytes specially, but i'll leave it as excercise.

using System;

namespace Tester
{
	class Class1
	{
		[STAThread]
		static void Main(string[] args)
		{
			const int repeats = 200;

			byte[] bytes1 = randomBytes(4 * 1000000);
			byte[] bytes2 = new byte[bytes1.Length];
			for (int i=0; i<bytes1.Length; i++)
				bytes1[i] = bytes2[i];


			// unsafe version
			int start_t = System.Environment.TickCount;
			for (int r=1; r<=repeats; r++)
			{
				bool res = CompareUnsafe(bytes1, bytes2);

				if (r == repeats)
					Console.WriteLine("Result: " + res);
			}
			int end_t = System.Environment.TickCount;
			Console.WriteLine("CompareUnsafe: {0} ms.", (end_t-start_t));

			// safe version
			start_t = System.Environment.TickCount;
			for (int r=1; r<=repeats; r++)
			{
				bool res = CompareSafe(bytes1, bytes2);

				if (r == repeats)
					Console.WriteLine("Result: " + res);
			}
			end_t = System.Environment.TickCount;
			Console.WriteLine("CompareSafe: {0} ms.", (end_t-start_t));
		}

		public static bool CompareSafe(byte[] byteArray1, byte[] byteArray2)
		{
			if (byteArray1.Length != byteArray2.Length)
				return false;

			for (int i=0; i<byteArray1.Length; i++)
				if (byteArray1[i] != byteArray2[i])
					return false;

			return true;
		}

		public unsafe static bool CompareUnsafe(byte[] byteArray1, byte[] byteArray2)
		{
			if (byteArray1.Length != byteArray2.Length)
				return false;

			if (byteArray1.Length % 4 != 0)
				throw new ArgumentException("Byte arrays have to be divisible by 4", "bytearray");

			fixed (byte* bytes1 = &(byteArray1[0]))
			fixed (byte* bytes2 = &(byteArray2[0]))
			{
				int* ints1 = (int*)bytes1;
				int* ints2 = (int*)bytes2;
				int size = byteArray1.Length / 4;
				for (int i=0; i<size; i++)
					if (ints1[i] != ints2[i])
						return false;

			}

			// all comparisons succeeded
			return true;
		}

		public static byte[] randomBytes(int size)
		{
			Random r = new Random();
			byte[] bytes = new byte[size];
			r.NextBytes(bytes);

			return bytes;
		}
	}
}

GeneralRe: memcmp in C# Pin
BrcKcc30-Jun-04 7:02
BrcKcc30-Jun-04 7:02 
QuestionWhats an RVA ? Pin
Peter Vertes29-Jun-04 8:27
Peter Vertes29-Jun-04 8:27 
AnswerRe: Whats an RVA ? Pin
Peter Vertes29-Jun-04 8:33
Peter Vertes29-Jun-04 8:33 
AnswerRe: Whats an RVA ? Pin
Heath Stewart29-Jun-04 9:49
protectorHeath Stewart29-Jun-04 9:49 
GeneralRe: Whats an RVA ? Pin
Peter Vertes29-Jun-04 10:28
Peter Vertes29-Jun-04 10:28 
GeneralTwo problems with MDI Pin
Metzler29-Jun-04 7:11
Metzler29-Jun-04 7:11 
GeneralUsing DataBinding with a user control Pin
Brett Slaski29-Jun-04 6:39
Brett Slaski29-Jun-04 6:39 
GeneralLDAP Active directory Pin
robmays29-Jun-04 4:11
robmays29-Jun-04 4:11 
GeneralRe: LDAP Active directory Pin
Heath Stewart29-Jun-04 5:37
protectorHeath Stewart29-Jun-04 5:37 
GeneralRe: LDAP Active directory Pin
robmays2-Jul-04 21:48
robmays2-Jul-04 21:48 
GeneralDataset Filter Interger Pin
Antonius_r329-Jun-04 4:11
Antonius_r329-Jun-04 4:11 
GeneralRe: Dataset Filter Interger Pin
Dave Kreskowiak29-Jun-04 5:04
mveDave Kreskowiak29-Jun-04 5:04 
GeneralRe: Dataset Filter Interger Pin
Antonius_r329-Jun-04 5:47
Antonius_r329-Jun-04 5:47 
GeneralRe: Dataset Filter Interger Pin
Antonius_r329-Jun-04 18:08
Antonius_r329-Jun-04 18:08 
GeneralRe: Dataset Filter Interger Pin
Orina DCosta29-Jun-04 20:20
Orina DCosta29-Jun-04 20:20 
GeneralRe: Dataset Filter Interger Pin
Antonius_r330-Jun-04 2:55
Antonius_r330-Jun-04 2:55 
GeneralRe: Dataset Filter Interger Pin
Antonius_r33-Jul-04 5:04
Antonius_r33-Jul-04 5:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.