为什么c ++文件中函数的位置会影响其性能?具体来说,在下面给出的示例中,我们有两个相同的功能,具有不同的,一致的性能配 如何调查这一点并确定性能如此不同?
这个例子很简单,因为我们有两个函数:a和b.每个都在紧密循环中运行多次并优化(-O3 -march=corei7-avx)和定时.这是代码:
#include <cstdint>
#include <iostream>
#include <numeric>
#include <boost/timer/timer.hpp>
bool array[] = {true, false, true, false, false, true};
uint32_t __attribute__((noinline)) a() {
asm("");
return std::accumulate(std::begin(array), std::end(array), 0);
}
uint32_t __attribute__((noinline)) b() {
asm("");
return std::accumulate(std::begin(array), std::end(array), 0);
}
const size_t WARM_ITERS = 1ull << 10;
const size_t MAX_ITERS = 1ull << 30;
void test(const char* name, uint32_t (*fn)())
{
std::cout << name << ": ";
for (size_t i = 0; i < WARM_ITERS; i++) …Run Code Online (Sandbox Code Playgroud) 我有一个自定义比较器,需要另一个比较器并应用额外的检查:
template <template <typename> class Comparator, typename T>
struct SoftOrder : public std::binary_function<T, T, bool> {
bool operator()(const T lhs, const T rhs) const {
return Comparator<T>()(lhs, rhs) && AnotherCheck();
}
};
Run Code Online (Sandbox Code Playgroud)
我有一个接受比较器的第二类,例如:
template <template <typename> class Comparator>
class Processor { ... };
Run Code Online (Sandbox Code Playgroud)
Processor使用标准比较器(例如std::less)可以很容易地实例化:
Processor<std::less> processor1;
Processor<std::greater> processor2;
Run Code Online (Sandbox Code Playgroud)
然而,实例化并不是那么容易,SoftOrder因为编译器正确地抱怨缺少第二个模板参数:
Processor<SoftOrder<std::less> > processor3; // <-- Fails to compile
Run Code Online (Sandbox Code Playgroud)
在发布此问题之前,我已经提出了一些解决方案.
template <typename T>
struct SoftOrderLessThan : public SoftOrder<std::less, T> {};
template <typename …Run Code Online (Sandbox Code Playgroud) 在C++ 11标准中,有一个关于支持统一初始化的数组的说明,其中指出:
如果可以如此分配具有相同初始化程序的显式数组,则实现可以在只读存储器中自由分配数组.
GCC/Clang/VS会利用这个吗?或者使用此功能的每个初始化是否受到堆栈上的其他数据的影响,以及此隐藏阵列的额外初始化时间?
例如,给出以下示例:
void function()
{
std::vector<std::string> values = { "First", "Second" };
...
Run Code Online (Sandbox Code Playgroud)
上面提到的每个编译器是否将后备数组存储在与声明的变量相同的内存中的统一初始化中static const?当调用函数或应用程序初始化时,每个编译器是否会初始化后备数组?(我不是在谈论std::initializer_list<std::string>将要创建的,而是它所指的"隐藏数组".
Intellisense需要很长时间来解析整个boost头文件,这是使Visual Studio 11难以使用的原因之一.特别是在我们公司的工作流程中,一次有多个分支机构.有没有办法:
c++ ×3
boost ×2
gcc ×2
boost-mpl ×1
c++11 ×1
clang ×1
intellisense ×1
performance ×1
templates ×1
visual-c++ ×1