我有幾個固定長度的數組,並且想要比較它們。可以在C++中比較原始內存嗎?
struct Foo
{
/// the data, not necessarily int and not necessarily length 32
int val[32];
/// simple way
inline bool compare1(const NAME_CELL & rhs) const
{
for (unsigned int ui = 0; ui < 32; ++ui)
{
if (val[ui] != rhs.val[ui])
{
return false;
}
}
return true;
}
/// compare memory directly instead of using a loop. Is this faster?
inline bool compare2(const NAME_CELL & rhs) const
{
return memcmp(val, rhs.val, 32 * sizeof(int)) == 0;
}
};
是比較1慢,快或等於比較2?有更快的方法嗎?
我假設你的意思是'memcmp'? –
此外,你的標題說「C」,但你已經標記了這個「C++」 - 你在說什麼? –
謝謝,更正了標題。我使用C++,但我的問題可能與C開發人員同樣相關。 – Fabian