整数VS指向布尔变量的数组复制 [英] Copying array of ints vs pointers to bools
问题描述
我工作的一个需要数组被复制数千/数百万次的程序。现在我已经重新$ P $的两种方式在阵列中psenting数据:
I'm working on a program that requires an array to be copied many thousands/millions of times. Right now I have two ways of representing the data in the array:
int数组:
int someArray[8][8];
其中,的someArray [A] [B]
可以是0,1,或2的值,或
where someArray[a][b]
can have a value of 0, 1, or 2, or
指针数组以布尔值:
bool * someArray[8][8];
其中,的someArray [A] [B]
可以是0(NULL指针),否则 *的someArray [A] [B]
可真(相当于1),或假(相当于2)。
where someArray[a][b]
can be 0 (null pointer), otherwise *someArray[a][b]
can be true (corresponding to 1), or false (corresponding to 2).
这数组将被复制更快(是的,如果我做了指针数组布尔,我会在每次我复制阵列时间宣布新的bool)?
Which array would be copied faster (and yes, if I made the pointers to booleans array, I would have to declare new bools every time I copy the array)?
推荐答案
这将复制快是跑题了,分配和释放条目,并取消引用指针检索每个价值,为你的 BOOL *
办法将淹没复制的成本。
Which would copy faster is beside the point, The overhead of allocating and freeing entries, and dereferencing the pointer to retrieve each value, for your bool*
approach will swamp the cost of copying.
如果你只是有3个可能的值,可以使用字符
的数组,这将复制4倍 INT
。 OK,这不是一个科学证明的语句,但该数组的会 4倍小。
If you just have 3 possible values, use an array of char
and that will copy 4 times faster than int
. OK, that's not a scientifically proven statement but the array will be 4 times smaller.
这篇关于整数VS指向布尔变量的数组复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!