它是更快地使用多个布尔值数组或位访问? [英] Is it faster to use an array or bit access for multiple boolean values?

查看:121
本文介绍了它是更快地使用多个布尔值数组或位访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)在一个32位CPU是它更快acccess 32布尔值的数组或一个字内访问32位? (假设我们想要查询的第N个元素的值,并且可以使用一个位掩码(第N位被设置)或整数N作为一个数组索引。)

1) On a 32-bit CPU is it faster to acccess an array of 32 boolean values or to access the 32 bits within one word? (Assume we want to check the value of the Nth element and can use either a bit-mask (Nth bit is set) or the integer N as an array index.)

在我看来,该数组会更快,因为所有常见的计算机体系结构本身在词汇层面的工作(32位,64位,等等,在并行处理),并访问子字位需要额外的工作。

It seems to me that the array would be faster because all common computer architectures natively work at the word level (32 bits, 64 bits, etc., processed in parallel) and accessing the sub-word bits takes extra work.

我知道不同的编译器将重新present不同的事情,但它似乎底层硬件架构,将决定答案。还是答案取决于语言和编译器?

I know different compilers will represent things differently, but it seems that the underlying hardware architecture would dictate the answer. Or does the answer depend on the language and compiler?

和,
2)如果此阵重新presents,我通过客户端和服务器之间的状态是速度答案逆转吗?
阅读问题<一的时候,这个问题就记href=\"http://stackoverflow.com/questions/516646/how-use-bit-bit-operator-to-control-object-state\">How使用bit /位操作员控制对象的状态?

P.S。是的,我可以写code,以测试这个自己,但随后的SO社会不会去一起玩!

P.S. Yes, I could write code to test this myself, but then the SO community wouldn't get to play along!

推荐答案

有关问题1:是的,在大多数32位平台,布尔值数组应该会更快,因为你将只是加载每个32位数组中-aligned价值和测试它反对0如果你使用一个字,你将所有的工作加位摆弄的开销。

For question #1: Yes, on most 32-bit platforms, an array of boolean values should be faster, because you will just be loading each 32-bit-aligned value in the array and testing it against 0. If you use a single word, you will have all that work plus the overhead of bit-fiddling.

有关问题#2:再次,是的,因为通过网络发送数据比在CPU和主存储器上的数据进行操作显著慢,送甚至一个字的开销将有力地胜过任何性能的利得或损失,你用得到调心字位摆弄。

For question #2: Again, yes, since sending data over a network is significantly slower than operating on data in the CPU and main memory, the overhead of sending even one word will strongly outweigh any performance gain or loss you get by aligning words or bit fiddling.

这篇关于它是更快地使用多个布尔值数组或位访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆