简体   繁体   English

返回间接类型对象会影响性能吗?

[英]Does returning a indirect typed object affect performance?

I have an inline function 我有一个内联函数

string myFunction() { return ""; }

Compared with 和....相比

string myFunction() { return string(); }

does it has a performance sacrifice? 它有性能牺牲吗?

Tested it on VC2012 release with std::string and QString (although on QString, the two returns different results : thanks to DaoWen). 使用std :: string和QString 在VC2012版本上测试它 (虽然在QString上,两者返回不同的结果:感谢DaoWen)。 Both show the second version is about 3 times faster than the first. 两者都显示第二个版本比第一个版本快3倍。 Thanks to all your answers and comments. 感谢您的所有答案和评论。 Tested code is attached below 测试代码附在下面

#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;
}

They will each cause different std::string constructors. 它们将导致不同的std :: string构造函数。

The std::string() -- will create an empty object. std :: string() - 将创建一个空对象。

The "" will construct using std::string(char*) “”将使用std :: string(char *)构造

The later will internally do a strlen+strcpy which is not needed in the first, so very small difference. 后者将在内部执行第一个不需要的strlen + strcpy,因此差异非常小。

In the example you posted, return ""; 在您发布的示例中, return ""; will be automatically translated to return string(""); 将自动翻译为return string(""); at compile time . 在编译时 Unless string("") is significantly slower than string() , then there shouldn't be much of a difference. 除非string("")明显慢于string() ,否则应该没有多大区别。


In your comment you mention that you're actually using QString , not std::string . 在你的评论中,你提到你实际上使用的是QString ,而不是std::string Note that QString() constructs a null string ( isNull() and isEmpty() both return true), whereas QString("") constructs an empty string ( isEmpty() returns true but isNull() returns false). 请注意, QString()构造一个空字符串isNull()isEmpty()都返回true),而QString("")构造一个空字符串isEmpty()返回true但isNull()返回false)。 They're not the same thing! 他们不是一回事! You can think of QString() like char * str = NULL; 您可以将QString() char * str = NULL; , and QString("") like char * str = ""; QString("")char * str = ""; .

Use return string(); 使用return string(); as that will use the default constructor. 因为那将使用默认构造函数。 A good Standard Library implementation will probably not even initialise the string buffer at that point. 一个好的标准库实现可能甚至不会在那时初始化字符串缓冲区。

The constructor from const char* must take a string copy. const char*的构造const char*必须采用字符串副本。 So I think return ""; 所以我觉得return ""; will be slower. 会比较慢。

But, to be really sure, race your horses . 但是,要确定,要比赛你的马匹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM