C++17で関数から値を返す手段によって、処理時間の差があるか実験
方式 | 所要時間 [ms] |
---|---|
値返し(RVO) | 15,448 |
出力引数(参照渡し) | 15,304 |
std::move | 15,301 |
⇒ 今回の評価環境においては差はなし。RVOに期待し、値返しで定義しまってよさそう。
下記関数をそれぞれ100,000回ずつ実行し、所要時間を計測
// 値返し (RVOに頼る)
std::vector<std::string> return_value()
{
return std::vector<std::string>(10'000, "test");
}
// 出力引数 (参照)
void output_argument(std::vector<std::string> &v)
{
v = std::vector<std::string>(10'000, "test");
}
// move
std::vector<std::string> return_move()
{
std::vector<std::string> v(10'000, "test");
return std::move(v);
}
-
Create a build directory:
mkdir build cd build
-
Run CMake and build:
cmake .. make
This will produce the executable file main
in the build
directory.
-
Execute:
./main