C++ 25 Feb 2012 13:19:21
C++ vector vs realloc()
A comparison of how many reallocations a worse case poorly coded use of realloc() does, compared to just using std::vector. 10000000 integers are added to the containers one-by-one. Clearly you would never abuse realloc() like this in real code, but it’s interesting nonetheless. Idea from sacrebleu at Freenode’s ##C++.
The full source is available in svn as a CMake project for easy cross-platform testing. Primary source vector-realloc.cpp
| Allocs / OS | std::vector | realloc() |
|---|---|---|
| Windows 7 | 40 | 9526 |
| Linux: Arch | 24 | 367 |
| Linux: Fedora 10 | 24 | 438 |
| Mac OS X | 24 | 31 |
- Mac OS X: OS X 10.7.3, 2.3 GHz Core i5, 8 GiB RAM. XCode 4.3, clang++ 3.1 -std=c++0x -stdlib=libc++ -O3.
- Windows 7: 64 bit, 1.60GHz Core i7 Q720, 8 GiB RAM. MSVC++ 2010 _SECURE_SCL=0
- Linux: Arch: VirtualBox on the Windows machine, VT-x, Arch Linux, kernel 3.2.7-1-ARCH x86_64, 1 GiB RAM. GNU g++ 4.6.2 -std=c++0x -O3
- Linux: Fedora 10: Fedora 10, kernel 2.6.27 x86_64, 2.66GHz Xeon, 8 GiB RAM. GNU g++ 4.4.1 -O3

