ここに私の毎日の仕事のコードがあります。我々はこの2を呼び出す場合ここ C++パフォーマンス:オブジェクトvs関数の参照渡し
// In class Price.hpp
private:
bool value = false;
// others
std::vector<int> vec;
...
public:
getBoolValue() { return value; }
setBoolValue(bool iValue) { value = iValue };
// a lot of setters & getters for the others
...
は、main.cppには
// First proposition
void Compute::computeAmount (const Price & iPrice) {
if (iPrice.getBoolValue()) {
// do something
}
}
// Second proposition
void Compute::computeAmount (const bool iValue) {
if (iValue ) {
// do something
}
}
Compute.cpp
ここ// In class Compute.hpp
// First proposition
void computeAmount (const Price & iPrice)
// Second proposition
void computeAmount (const bool iValue)
Compute.hpp
である:ここでは は、クラスの価格でありますこのように異なる機能:Compute aCompute;
Price aPrice;
// Do a lot of set for those two
....
// Discussion
aCompute.computeAmount (aPrice);
aCompute.computeAmount (aPrice.getBoolValue());
価格は非常に大きいオブジェクトです。したがって、パフォーマンス(大規模オブジェクトの参照渡しとそのメンバ関数の戻り値による渡し)については、どちらがより効率的であるとお考えですか?私はそれが同じで、パフォーマンス面で違いはないと言います。
お願いします! – user0042
"あなたの意見はより効率的ですか?_"この場合はなぜ重要なのですか?それをプロファイリングして、より効率的であるあなた自身のために見てください。 –
独自のプロファイラを作成し、測定します。シンプルなプロファイラを書くのは簡単です – Asesh