将ints和指针的数组复制到bools [英] Copying array of ints vs pointers to bools
问题描述
我正在开发一个程序,需要一个数组被复制数千/数百万次。现在我有两种方法来表示数组中的数据:
一个int数组:
int someArray [8] [8];
其中 someArray [a] [b]
可以具有值0,1或2,或
布尔值指针数组:
bool * someArray [8] [8];
其中 someArray [a] [b]
可以为0(空指针),否则 * someArray [a] [b]
可以为true(对应于1) >
哪个数组会被复制得更快(是的,如果我把指针指向booleans数组,我必须在每次复制数组时声明新的bool)?
哪一个复制更快是不可能的,分配和释放条目的开销,解除引用指针检索每个值, code> bool * 方法会淹没复制的成本。
如果你只有3个可能的值,使用一个 char
数组, code> int 。 OK,这不是科学证明的声明,但数组会小4倍。
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:
An array of ints:
int someArray[8][8];
where someArray[a][b]
can have a value of 0, 1, or 2, or
An array of pointers to booleans:
bool * someArray[8][8];
where someArray[a][b]
can be 0 (null pointer), otherwise *someArray[a][b]
can be true (corresponding to 1), or false (corresponding to 2).
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)?
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.
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.
这篇关于将ints和指针的数组复制到bools的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!