Is there other way of comparing two byte arrays
than iterating through the two and compare individual bytes ?
Oleg Subachev 5 40102 Is there other way of comparing two byte arrays than iterating through the two and compare individual bytes ?
No, but you can always start by checking that their lengths are equal
first.
Mattias
--
Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Hi,
Nop, the same goes with any kind of array or collections
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Oleg Subachev" <ol**@urvb.ru > wrote in message
news:ux******** ******@TK2MSFTN GP02.phx.gbl... Is there other way of comparing two byte arrays than iterating through the two and compare individual bytes ?
Oleg Subachev
On Tue, 18 Apr 2006 10:52:08 +0600, "Oleg Subachev" <ol**@urvb.ru >
wrote: Is there other way of comparing two byte arrays than iterating through the two and compare individual bytes ?
Oleg Subachev
In most cases it is enough to just iterate and compare each byte. In
the rare case that maximum performance is needed you could use unsafe
code to speed up the process by using int pointers.
Here is a small code snippet that demonstrates the basics. It will
only work if the array length is divisible by 4.
public unsafe static bool Compare(byte[] b1, byte[] b2)
{
fixed (byte* bp1 = b1)
{
fixed (byte* bp2 = b2)
{
int* ip1 = (int*)bp1;
int* ip2 = (int*)bp2;
for (int i = 0; i < b1.Length / 4; i++)
{
if (ip2[i] != ip1[i])
return false;
}
return true;
}
}
}
--
Marcus Andrén
Marcus Andrén wrote: Here is a small code snippet that demonstrates the basics. It will only work if the array length is divisible by 4.
public unsafe static bool Compare(byte[] b1, byte[] b2) { fixed (byte* bp1 = b1) { fixed (byte* bp2 = b2) { int* ip1 = (int*)bp1; int* ip2 = (int*)bp2;
for (int i = 0; i < b1.Length / 4; i++)
remember checking the length of b2 also, especially since this is unsafe
code. I would like the function to start with something like:
if ( b1.Length % 4 != 0 )
throw new ArgumentExcepti on("Length % 4 != 0", "b1");
if ( b2.Length % 4 != 0 )
throw new ArgumentExcepti on("Length % 4 != 0", "b2");
if ( b1.Length != b2.Length )
return false;
{ if (ip2[i] != ip1[i]) return false; } return true; } } }
How much, and how have you measured that the above is faster, than simply
public static bool Compare(byte[] b1, byte[] b2) {
if ( b1.Length != b2.Length )
return false;
for ( int i = 0; i < b1.Length; ++i )
if ( b1[i] != b2[i] )
return false;
return true;
}
--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
On Wed, 19 Apr 2006 07:42:43 +0200, Helge Jensen
<he**********@s log.dk> wrote: remember checking the length of b2 also, especially since this is unsafe code. I would like the function to start with something like:
Very true.
How much, and how have you measured that the above is faster, than simply
I had done some basic measurements earlier, but since you asked I got
a little more curious and did some more detailed testing including
what happens if the arrays doesn't match in the first few bytes of the
comparison.
These are simple timings. Ratios measured as Unsafe:Safe
match 1000 bytes 1 : 3.9
match 20 bytes 1 : 2.1
match 4 bytes 1 : 1.05
1st byte fail 1 : 0.45
2nd byte fail 1 : 0.6
3rd byte fail 1 : 0.7
4th byte fail 1 : 0.9
5th byte fail 1 : 0.95
When large parts of big arrays has to be compared the difference in
performance approaches 4. This is pretty much expected since the byte
version has to loop and compare 4 times as much.
With byte arrays of size 20 the unsafe version is still twice as fast.
With a really small array, and/or if the comparisons fail in the first
few bytes the advantage turns to the safe version. If the first byte
fails the safe version is more than twice as fast. The overhead of the
unsafe version is too large.
I also can't explain why the "match 4 bytes" differs slightly from
"4th byte fail". From just looking at the code I expected them to
perform the same.
--
Marcus Andrén This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Einar |
last post by:
Hi,
I wonder if there is a nice bit twiddling hack to compare a large
number of variables?
If you first store them in an array, you can do:
for (i = 0; i < n; i++) {
if (array != value) {
/* array differs from value, do something*/
|
by: Jim H |
last post by:
I will have many 3 byte Byte arrays. Is there a way to compare the values
without iterating through and comparing each element in the code?
Example:
byte lTest1 = new byte {0x1, 0x2, 0x3};
byte lTest2 = new byte {0x1, 0x2, 0x3};
byte lTest3 = new byte {0x1, 0x5, 0x3};
if(Comparer.Equals(lTest1, lTest2))
System.Diagnostics.Trace.WriteLine("Test1 and Test2 are the same",
"Debug");
|
by: NathanV |
last post by:
I'm trying to compare two byte's. I think I have to use binary operators.
How can I tell if two bytes have the same value?
Also, I have a function that is returning a byte value. Is there a way to
see if the return value isnt null.
Thanks!
|
by: nobs |
last post by:
Hi
I have two byte arrays (each with a length of 8 bytes)
an I'd like to compare them if they are equal.
The operator == shouldn't work because its not a basic Data Type
so I used the method Equal, but it also isn't working.
right know I use
if(mykey.Length!= keytocheck.Length)
|
by: Jacob Thastrup |
last post by:
Hi is there an easy way to compare arrays without going through each
entry in the arrays?
Say I have two arrays and I want to find entries that are not present in
both.
Thanks
Jacob Thastrup
| |
by: rcolby |
last post by:
Evening,
Wondering if someone can point me in the right direction, on how I would
compare a system.guid with a system.byte.
system.guid (pulled from sql server table with a data type of
uniqueidentifier, originally taken from objectGUID from active directory
domain)
|
by: Tom |
last post by:
What's the best way to compare two byte arrays? Right now I am converting
them to base64 strings and comparing those, as so:
'result1 and result2 are two existing byte arrays that have been loaded
Dim data64_1 As String = System.Convert.ToBase64String(result1, 0,
result1.Length)
Dim data64_2 As String = System.Convert.ToBase64String(result2, 0,
result2.Length)
Debug.WriteLine(IIf(data64_1 = data64_2, "Equals", "NOT Equals"))
|
by: Turbot |
last post by:
Hello,
Anyone know how to compare two byte arrays without going through each
item in the array. I actually want to compare two bitmap objects to see
if they both contain the same picture bit have been unable to do this.
I figured if I converted them to byte arrays there would be a simple
way of comparing them but I have had no luck.
Thanks in advance.
|
by: Ole |
last post by:
How do I compare two byte arrays in a if statement?
Thanks
Ole
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |