可以在C++中比较原始内存
Is is possible to compare raw memory in 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?有没有更快的方法?
除非你已经初始化了val
的元素,否则任何一种方法的行为在标准C++中都是未定义的。
撇开这一点不谈,最好的办法是相信编译器会做出适当的优化。但是,如果您仍然有疑问,请(i(分析性能,(ii(检查生成的程序集,等等。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么比较运算符如此快速
- 我可以使用 g++ 进行三种比较 (<=>) 吗?
- 比较字符数组
- 将模板化的类型与C++中的某些类/类型进行比较
- C++自定义比较函数
- 如何比较自定义类的std::变体
- 将浮动的heightmap数组导出为16位原始值
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- std::设置自定义比较器
- 布尔比较运算符是如何在C++中工作的
- 给定一个向量,如何找到该向量的所有子集和的原始索引
- 可以在C++中比较原始内存
- C 拖拖与原始角色的比较失败
- std::排序比较器,可以看到元素的(原始)索引
- 如何用自定义比较表示一组指针,但保持原始原始指针的重复比较
- 如何打印QImage的所有原始数据,以便在内存中进行比较
- winpcap/libpcap与原始套接字的比较
- 将拓扑排序列表与原始列表进行比较
- std::函数与原始函数指针和void* this的性能比较