将结构与零进行比较的首选方法

发布于 2024-12-28 21:17:42 字数 456 浏览 0 评论 0原文

今天我遇到了一种情况,我需要确定由大约 40 个元素组成的整个结构是否为零 - 意味着每个元素都为零。
在思考如何使其尽可能快速高效时,我想到了 3 种不同的方法:

  1. 将每个元素与零进行比较,生成 40 个 if 语句。
  2. 分配一个类似的结构,该结构已全部归零,并使用该结构memcmp它。
  3. 将结构包装在一个联合中,类型足够大以覆盖所有结构。

例如

typedef union {
  struct {
    uint8_t a;
    uint8_t b;
    }
  uint16_t c;
 } STRUCTURE_A;

,然后将其与零进行比较。

我想知道您对这些解决方案的看法,您认为其中哪些解决方案最快、最有效。
如果您有更好的方法,请告诉我...
谢谢。

Today I came across a situation where I needed to decide if an entire structure that consists of about 40 elements is zero - meaning that each of the elements is zero.
When thinking how to make it as fast and efficient as possible, I thought of 3 different ways to do so:

  1. compare each element to zero, resulting 40 if statements.
  2. allocating a similar structure which is allready zeroed and memcmp it with the structure.
  3. wrapping the structure in a union with a type big enough to cover all of it.

for instance

typedef union {
  struct {
    uint8_t a;
    uint8_t b;
    }
  uint16_t c;
 } STRUCTURE_A;

and then comparing it to zero.

I would like to know what you think about these solutions, which of them you find the fastest and the most efficient.
And if you thing of a better approach please tell me...
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清眉祭 2025-01-04 21:17:42

将结构体的每个成员与 0 进行比较。

这是比较两个结构体对象的唯一安全方法(即使其中一个结构体对象将所有成员设置为值 0)。不要使用memcmp来比较结构体,结构体中填充字节的值是未指定的。另请注意,不允许将 == 运算符与结构对象操作数一起使用。

请参阅有关结构对象比较的 c-faq 链接:

问:有没有办法自动比较结构?< /a>

Compare every member of the structure to 0.

This is the only safe way to compare two structures objects (even if one of the structure objects has all members set to the value 0). Don't use memcmp to compare a struct, the value of the padding's bytes in the structure is unspecified. Note also that it is not permitted to use the == operator with structure objects operands.

See this c-faq link on structure object comparison:

Q: Is there a way to compare structures automatically?

〗斷ホ乔殘χμё〖 2025-01-04 21:17:42

如果您的结构大小 <= 处理器的字大小,您可以执行联合技巧,但是,任何好的编译器都应该自动执行此操作,也就是说,它会压缩 if 的,从而允许清晰度,但仍保持性能达到标准。

If your structure size is <= the word size of the processor, you can do your union trick, however, any good compiler should do this automatically, aka it would compact the if's, allowing for clarity but still keeping performance up to scratch.

流心雨 2025-01-04 21:17:42

为了代码清晰,正如其他人指出的那样,为了避免填充引起的问题,最好检查每个成员。

为了速度,从这样的开始,只检查每个字节以查看它是否为零。

int iszero(void * ptr, int bytes )
{
   char * bptr = (char*)ptr;
   while( bytes-- )
     if( *bptr++ )
         return 0;
  return 1;
}

然后优化以进行字对齐比较。查看 newlib 对 strlen() & 等内容的实现。 memcpy() 有关如何完成此操作的示例。

For code clarity, and as others have pointed out, to avoid problems caused by padding, checking each member would be best.

For speed, start with something like this that just checks each byte to see if it's zero.

int iszero(void * ptr, int bytes )
{
   char * bptr = (char*)ptr;
   while( bytes-- )
     if( *bptr++ )
         return 0;
  return 1;
}

Then optimize to do word-aligned comparisons. Check out newlib's implementation of things like strlen() & memcpy() for examples on how that is done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文