Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::stringstream的简单但频繁的使用是否是过早的悲观?_C++_Performance_C++11_Stl - Fatal编程技术网

C++ std::stringstream的简单但频繁的使用是否是过早的悲观?

C++ std::stringstream的简单但频繁的使用是否是过早的悲观?,c++,performance,c++11,stl,C++,Performance,C++11,Stl,我有一个简单的场景。我需要将两个C字符串连接到一个std::string。我决定采用以下两种方式之一: 解决方案1 解决方案2 void ProcessEvent(char const*pName){ std::ostringstream ss; 让我们来测量它 具有以下功能的快速测试: void func1(const char* text) { std::string s; s.reserve(50); s += "com.domain.event."; s

我有一个简单的场景。我需要将两个C字符串连接到一个
std::string
。我决定采用以下两种方式之一:

解决方案1 解决方案2
void ProcessEvent(char const*pName){
std::ostringstream ss;
让我们来测量它
具有以下功能的快速测试:

void func1(const char* text) {
    std::string s;
    s.reserve(50);
    s += "com.domain.event.";
    s += text;
}

void func2(const char* text) {
    std::ostringstream oss;
    oss << "com.domain.event." << text;
    std::string s = oss.str();
}
函数3:27毫秒


最简单的解决方案通常是最快的。

您没有提到第三种选择,即不在字符串中预先分配任何内容,只让优化器做它最擅长的事情

给定这两个函数,
func1
func3

void func1(const char* text) {
    std::string s;
    s.reserve(50);
    s += "com.domain.event.";
    s += text;
    std::cout << s;
}

void func3(const char* text) {
    std::string s;
    s += "com.domain.event.";
    s += text;
    std::cout << s;
}
void func1(常量字符*文本){
std::字符串s;
s、 储备(50);
s+=“com.domain.event。”;
s+=文本;

std::cout我建议做一些分析,看看这是否真的是一个值得关注的问题。如果这真的是一个瓶颈,
std::string
也不是最理想的解决方案。解决方案1对我来说似乎更自然(少了
reserve()
对于这种情况。我通常只在需要转换数字或用户定义的类型时才考虑使用
std::stringstreams
。虽然您不应该过早地进行优化,但我认为您仍然应该注意效率。解决方案3:
std::string fullName=std::string{“com.domain.events”。}+std::string{pName}
--有很多方法可以剥这只猫的皮。尽可能简单一些。你也可以在codereview.stackexchange.com上获得一些有趣的意见。或者在C++14中,
std::string fullName=“com.domain.events。”s+pName;
。尽可能简单。希望我能为您在自己的机器上进行实际测试以及我应该想到的优秀解决方案3提供不止一个+1!谢谢!很高兴能提供帮助,尽管解决方案3的功劳应该归于:)
void func1(const char* text) {
    std::string s;
    s.reserve(50);
    s += "com.domain.event.";
    s += text;
}

void func2(const char* text) {
    std::ostringstream oss;
    oss << "com.domain.event." << text;
    std::string s = oss.str();
}
void func3(const char* text) {
    std::string s = "com.domain.event" + std::string{text};
}
void func1(const char* text) {
    std::string s;
    s.reserve(50);
    s += "com.domain.event.";
    s += text;
    std::cout << s;
}

void func3(const char* text) {
    std::string s;
    s += "com.domain.event.";
    s += text;
    std::cout << s;
}
leaq    16(%rsp), %rdi
movl    $50, %esi
call    std::basic_string<char>::append(char const*, unsigned long)