4
我有一個自定義循環緩衝區實現,它使用通過new []
分配的正常數組,然後使用std::move
將元素移動到數組中。這裏是我push()
方法的實現:std :: move()作爲性能瓶頸?
void push(value_type&& value)
{
_content[_end] = std::move(value); // 9.2% of execution is spend here
increment(); // 0.6% here
}
我移動到陣列中的對象基本上只是一個指針和std::unique_ptr
:
struct Task
{
Task()
{}
Function function;
Batch *batch;
};
而且Function
看起來是這樣的:
class Function
{
public:
template<typename F>
Function(F&& f) :
_implementation(new ImplementationType<F>(std::move(f)))
{}
void operator()() { _implementation->Call(); }
Function() = default;
Function(Function&& other) :
_implementation(std::move(other._implementation))
{}
Function& operator=(Function&& other)
{
_implementation = std::move(other._implementation);
return *this;
}
Function(const Function&) = delete;
Function(Function&) = delete;
Function& operator= (const Function&) = delete;
private:
struct Base
{
virtual void Call() = 0;
virtual ~Base() {}
};
template<typename F>
struct ImplementationType : Base
{
ImplementationType(F&& f) :
function(std::move(f))
{}
void Call()
{
function();
}
F function;
};
std::unique_ptr<Base> _implementation;
};
我在一個循環中重複調用環形緩衝區push()
方法來填充緩衝區中的任務,沒有其他計算在那裏發生。我期望std::move()
的開銷很小,絕對不會佔用我計算時間的最大部分。任何人都可以指出我在這裏做錯的正確方向嗎?
你是如何確定這一點的? 「std :: move」調用不應該在任何值得使用的編譯器的優化器中生存。 –
我在程序中附加了'Instruments',並捕獲了大約兩分鐘的運行時間。它標誌着我將數據移動到數組中的行,執行時間的9.2%(如註釋中所標記的)。用'-Os'編譯,沒有使用Xcode 5附帶的最新Clang留下的調試內容或運行時斷言。 – JustSid
它不是'std :: move'。這是你的對象的移動構造函數。我不確定你在這裏期望的是什麼:如果你什麼都不做,只是把東西放入緩衝區,把東西放入緩衝區(即移動它)將是最常見的活動。 –