字节数组比较 - 速度 [英] Byte array compare - Speed
问题描述
嘿伙计们,这是我在这里的第一篇文章,所以我只会说你好,我好!
好的,我的问题很多。
是否有更快的方法将1字节数组与另一字节数组进行比较?
这是我目前的代码
/ /查看比赛
match = true;
for(int i = 0; i< arraylen; i ++)
{
if(data [i]!= data2 [i])
{
match = false;
休息;
}
}
先谢谢你们!
MrCoder发布:
嘿伙计们,我在这里发表的第一篇文章,所以我只会说你好,我是谁!
好的,我的问题很多。
是否有更快的方法将1字节数组与另一个数组进行比较?
这是我目前的代码
//检查匹配
m atch = true;
for(int i = 0;我< arraylen; i ++)
{if(data [i]!= data2 [i])
{
匹配= false;
休息;
}
先谢谢你们!!
如果数组是可变长度的,如上例所示,那么我会说/是
说是的,你的代码非常有效,尽管可能有一些
内存函数我不知道这可以更好地完成工作吗?
如果数组的长度不变,我会建议如下:
Struct ChocolateArray
{
int monkey [50];
};
int main(无效)
{
bool match;
ChocolateArray aa;
ChocolateArray bb;
//与阵列混乱
if(aa == bb)
{
match = true;
}
else
{
match = false;
}
}
-JKop
On Sun,2004年6月6日09:23:28 +0000(UTC),MrCoder <先生***** @ hotmail.com>写道:
嘿伙计们,我在这里发表的第一篇文章,所以我只会说Hello everbody!
是否有更快的方法将1字节数组与另一个数组进行比较?
这是我目前的代码
/> //检查匹配
match = true;
for(int i = 0; i< arraylen; i ++)
{
如果(data [i]!= data2 [i])
{
匹配=假;
>休息;
}
先谢谢你们!!
>
memcmp(data,data2,arraylen);
MrCoder写道:嘿伙计们,我的第一篇文章在这里,所以我只会说Hello everbody!
好的,我的问题很多。
是否有更快的方法来比较1字节数组到另一个?
这是我目前的代码
//检查匹配
match = true;
for(int i = 0;我< arraylen; i ++)
{if(data [i]!= data2 [i])
{
匹配= false;
休息;
}
}
在大多数系统上,memcmp可能会很好地优化,它可能(在一些
平台上)被编译器内联。
ie
#include< cstring>
bool isequal(const char * a,const char * b,int len)
{
return :: memcmp(a,b,len)== 0;
}
使用gcc:g ++ -mtune = i686 -O3生成:
memcmp_tst.o:文件格式elf32-i386
部分的反汇编.text:
00000000< _Z7isequalPKcS0_i> :
0:55推%ebp
1:89 e5 mov%esp,%ebp
3:83 ec 0c sub
Hey guys, my first post on here so I''ll just say "Hello everbody!"
Ok heres my question for you lot.
Is there a faster way to compare 1 byte array to another?
This is my current code
// Check for a match
match = true;
for ( int i = 0; i < arraylen; i++)
{
if( data[i] != data2[i] )
{
match = false;
break;
}
}
Thanks in advance guys!!
MrCoder posted:
Hey guys, my first post on here so I''ll just say "Hello everbody!"
Ok heres my question for you lot.
Is there a faster way to compare 1 byte array to another?
This is my current code
// Check for a match
match = true;
for ( int i = 0; i < arraylen; i++)
{
if( data[i] != data2[i] )
{
match = false;
break;
}
}
Thanks in advance guys!!
If the array is of a variable length, as in your above example, then I would
say that Yes, your code is pretty efficent, although maybe there''s some
memory functions I''m unaware of that could do the job better?
If the array is of constant length, I''d suggest the following:
Struct ChocolateArray
{
int monkey[50];
};
int main(void)
{
bool match;
ChocolateArray aa;
ChocolateArray bb;
//Mess with the arrays
if (aa == bb)
{
match = true;
}
else
{
match = false;
}
}
-JKop
On Sun, 6 Jun 2004 09:23:28 +0000 (UTC), "MrCoder" <Mr*****@hotmail.com> wrote:
Hey guys, my first post on here so I''ll just say "Hello everbody!"
Ok heres my question for you lot.
Is there a faster way to compare 1 byte array to another?
This is my current code
// Check for a match
match = true;
for ( int i = 0; i < arraylen; i++)
{
if( data[i] != data2[i] )
{
match = false;
break;
}
}
Thanks in advance guys!!
memcmp( data, data2, arraylen );
MrCoder wrote:Hey guys, my first post on here so I''ll just say "Hello everbody!"
Ok heres my question for you lot.
Is there a faster way to compare 1 byte array to another?
This is my current code
// Check for a match
match = true;
for ( int i = 0; i < arraylen; i++)
{
if( data[i] != data2[i] )
{
match = false;
break;
}
}
On most systems, memcmp may be nicely optimized, it may (on some
platforms) be inlined by the compiler.
i.e.
#include <cstring>
bool isequal( const char * a, const char * b, int len )
{
return ::memcmp( a, b, len ) == 0;
}
using gcc: g++ -mtune=i686 -O3 generates:
memcmp_tst.o: file format elf32-i386
Disassembly of section .text:
00000000 <_Z7isequalPKcS0_i>:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 83 ec 0c sub
这篇关于字节数组比较 - 速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!