我正在阅读 Why is there no reallocation functionality in C++ allocators?和 Is it possible to create an array on the heap at run-time, and then allocate more space whenever needed? ,这清楚地表明不可能重新分配动态对象数组。
然而,在 Josuttis 的 The C++ Standard Library 中,它声明了一个分配器,allocator
,有一个函数 allocate
,语法如下
pointer allocator::allocate(size_type num, allocator<void>::pointer hint = 0)
其中 hint
具有实现定义的含义,可用于帮助提高性能。
是否有利用此优势的任何实现?
最佳答案
在我的 plf::colony 中,我在小型标量类型的迭代时间上获得了显着的性能优势c++ 容器在 Visual Studio 2010-2013 下使用带有 std::allocator 的提示(迭代速度提高了约 21%),在 GCC 5.1 下的加速比小得多。所以可以肯定地说,对于那些编译器和 std::allocator,它会有所作为。但差异将取决于编译器。我不知道提示忽略分配器与提示观察分配器的比率。
关于c++ - 分配提示是否被使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15380318/