私はインライン関数を持っています
string myFunction() { return ""; }
と比べて
string myFunction() { return string(); }
パフォーマンスが犠牲になりますか?
std::string と QString を使用して VC2012 リリースでテストしました (ただし、QString では、2 つは異なる結果を返します: DaoWen のおかげです)。どちらも、2 番目のバージョンが最初のバージョンよりも約 3 倍高速であることを示しています。すべての回答とコメントに感謝します。テスト済みのコードを以下に添付します
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
inline string fa() { return ""; }
inline string fb() { return string(); }
int main()
{
int N = 500000000;
{
clock_t begin = clock();
for (int i = 0; i < N; ++i)
fa();
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
cout << "fa: " << elapsed_secs << "s \n";
}
{
clock_t begin = clock();
for (int i = 0; i < N; ++i)
fb();
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
cout << "fb: " << elapsed_secs << "s \n";
}
return 0;
}