我在各种编译器中测试向量与数组的性能。特别是,在以下程序中使用数组版本时,clang似乎很糟糕:
#include <iostream>
#include <numeric>
#include <vector>
#include <limits>
int main(int argc, char* argv[])
{
#ifdef VECTOR
std::vector<char> v(std::numeric_limits<int>::max() / 10);
std::iota(v.begin(), v.end(), 0);
#endif
#ifdef ARRAY
char array[std::numeric_limits<int>::max() / 10];
std::iota(std::begin(array), std::end(array), 0);
#endif
return 0;
}
以下是一些示例输出:
g++ use vector
real 0m0.561s
user 0m0.216s
sys 0m0.344s
g++ use array
real 0m0.005s
user 0m0.000s
sys 0m0.004s
clang use vector
real 0m0.554s
user 0m0.176s
sys 0m0.372s
clang use array
bash: line 10: 3416 Segmentation fault (core dumped) ./a.out
real 0m1.822s
user 0m0.000s
sys 0m0.004s
我知道我可以增加默认的堆栈限制,但我的问题是,为什么clang只会废话而其他人不会?
Valgrind输出:
==4201== Warning: client switching stacks? SP change: 0x7ff0008d8 --> 0x7f2333c80
==4201== to suppress, use: --max-stackframe=214748248 or greater
==4201== Invalid write of size 8
==4201== at 0x40070D: main (in /tmp/1412787937.5536/a.out)
==4201== Address 0x7f2333c00 is on thread 1's stack
==4201==
==4201==
==4201== Process terminating with default action of signal 11 (SIGSEGV)
==4201== Access not within mapped region at address 0x7F2333C08
==4201== at 0x40070D: main (in /tmp/1412787937.5536/a.out)
==4201== If you believe this happened as a result of a stack
==4201== overflow in your program's main thread (unlikely but
==4201== possible), you can try to increase the size of the
==4201== main thread stack using the --main-stacksize= flag.
==4201== The main thread stack size used in this run was 8388608.
==4201==
==4201== Process terminating with default action of signal 11 (SIGSEGV)
==4201== Access not within mapped region at address 0x7F2333C79
==4201== at 0x4A255A0: _vgnU_freeres (in /usr/lib/valgrind/vgpreload_core-amd64-linux.so)
==4201== If you believe this happened as a result of a stack
==4201== overflow in your program's main thread (unlikely but
==4201== possible), you can try to increase the size of the
==4201== main thread stack using the --main-stacksize= flag.
==4201== The main thread stack size used in this run was 8388608.