今天我谈论的是 C++11 中的新闻功能,如线程、to_string 和 stoi。

但事实上,这一切在 C++98 中都已经成为可能。

然后我决定比较旧库与新闻库:

C++11:


#include <iostream>
#include <string>
#include <ctime>

using namespace std;

int main()
{
    clock_t tStart = clock();
    string input = "50";

    for (int i = 0; i < 50000; i++)
    {
        int number = stoi(input);
    }

    cout << (double)(clock() - tStart) << endl;
    return 0;
}

C++98


#include <iostream>
#include <string>
#include <sstream>
#include <ctime>

using namespace std;

int main()
{
    clock_t tStart = clock();
    string input = "50";
    stringstream ss;

    for (int i = 0; i < 50000; i++)
    {
        int number;
        ss << input;
        ss >> number;
    }

    cout << (double)(clock() - tStart) << endl;
    return 0;
}

屏幕截图:

Windows 7 轻型服务器:

c&#43;&#43; - 为什么 stoi 比没有 -O3 的 stringstream 慢得多?-LMLPHP

c&#43;&#43; - 为什么 stoi 比没有 -O3 的 stringstream 慢得多?-LMLPHP

Ubuntu 14.04:

c&#43;&#43; - 为什么 stoi 比没有 -O3 的 stringstream 慢得多?-LMLPHP

c&#43;&#43; - 为什么 stoi 比没有 -O3 的 stringstream 慢得多?-LMLPHP

Slackware Server 14.1 通过 SSH:

c&#43;&#43; - 为什么 stoi 比没有 -O3 的 stringstream 慢得多?-LMLPHP

c&#43;&#43; - 为什么 stoi 比没有 -O3 的 stringstream 慢得多?-LMLPHP

优化

Windows 7 轻型服务器:

c&#43;&#43; - 为什么 stoi 比没有 -O3 的 stringstream 慢得多?-LMLPHP

c&#43;&#43; - 为什么 stoi 比没有 -O3 的 stringstream 慢得多?-LMLPHP

Ubuntu 14.04:

c&#43;&#43; - 为什么 stoi 比没有 -O3 的 stringstream 慢得多?-LMLPHP

c&#43;&#43; - 为什么 stoi 比没有 -O3 的 stringstream 慢得多?-LMLPHP

Slackware Server 14.1 通过 SSH:

c&#43;&#43; - 为什么 stoi 比没有 -O3 的 stringstream 慢得多?-LMLPHP

c&#43;&#43; - 为什么 stoi 比没有 -O3 的 stringstream 慢得多?-LMLPHP

配置:

Windows 7 轻型服务器:Intel(R) Core(TM) i5-4590 CPU @ 3.30GHz

Ubuntu 14.04:Intel(R) Core(TM) i7-2670QM CPU @ 2.20GHz

Slackware Server 14.1 通过 SSH:Intel(R) Core(TM) i3-4150 CPU @ 3.50GHz

问题:

stoi 和 stringstream 有什么区别?

为什么以及何时在字符串库(stoi,to_string)中使用新闻函数?

最后...... stoi 使用 -O3 快得多,而 sstream 则不然。那么,与 sstream 相比,-O3 对 stoi 的作用是什么?

最佳答案

我刚刚检查了以下内容:

gcc -v:

Using built-in specs.
COLLECT_GCC=x86_64-alt-linux-gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-alt-linux/5/lto-wrapper
Target: x86_64-alt-linux
Configured with: ../configure --host=x86_64-alt-linux --build=x86_64-alt-linux --target=x86_64-alt-linux --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc --datadir=/usr/share --includedir=/usr/include --libdir=/usr/lib64 --libexecdir=/usr/libexec --localstatedir=/var/lib --sharedstatedir=/var/lib --mandir=/usr/share/man --infodir=/usr/share/info --disable-dependency-tracking --without-included-gettext --enable-shared --program-suffix=-5 --with-slibdir=/lib64 --with-bugurl=http://bugzilla.altlinux.org --enable-__cxa_atexit --enable-threads=posix --enable-checking=release --with-system-zlib --without-included-gettext --enable-multilib --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --with-arch_32=i586 --with-tune_32=generic --with-multilib-list=m64,m32,mx32 --enable-bootstrap --enable-languages=c,c++,fortran,objc,obj-c++,java,ada,go,lto --enable-plugin --enable-java-awt=gtk --with-native-libdir=/usr/lib64/gcj-5 --with-ecj-jar=/usr/share/java/ecj.jar --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-1.5.0.0/jre --enable-libgcj-multifile --disable-libjava-multilib --enable-java-maintainer-mode
Thread model: posix
gcc version 5.3.1 20151207 (ALT Linux 5.3.1-alt1) (GCC)

编码:
#include <iostream>
#include <string>
#include <chrono>
#include <sstream>

using namespace std;
using namespace chrono;

void stringstream_test(const string& input, int repetitions) {
    stringstream ss;
    auto start = steady_clock::now();
    for (int i = 0; i < repetitions; i++) {
        int number;
        ss << input;
        ss >> number;
    }

    auto end = steady_clock::now();
    auto dur = duration_cast<nanoseconds>(end - start);
    cout << "stringstream_test completed in " << dur.count() << " nanoseconds." << endl;
}

void stoi_test(const string& input, int repetitions) {
    auto start = steady_clock::now();
    for (int i = 0; i < repetitions; i++) {
        int number = stoi(input);
    }
    auto end = steady_clock::now();
    auto dur = duration_cast<nanoseconds>(end - start);
    cout << "stoi_test         completed in " << dur.count() << " nanoseconds." << endl;
}

int main() {
    stringstream_test("50", 500000);
    stoi_test("50", 500000);
    return 0;
}

编译为 c++ -std=c++11 -O3 -o stoi_perf stoi_perf.cc

结果,使用 while true; do ./stoi_perf; done 接收并在重复几次后停止:
stringstream_test completed in 10449080 nanoseconds.
stoi_test         completed in 10437559 nanoseconds.
stringstream_test completed in 9074869 nanoseconds.
stoi_test         completed in 9895661 nanoseconds.
stringstream_test completed in 11516788 nanoseconds.
stoi_test         completed in 12266627 nanoseconds.
stringstream_test completed in 10017085 nanoseconds.
stoi_test         completed in 9468441 nanoseconds.
stringstream_test completed in 9957401 nanoseconds.
stoi_test         completed in 9483004 nanoseconds.
stringstream_test completed in 10069845 nanoseconds.
stoi_test         completed in 9657653 nanoseconds.
stringstream_test completed in 9568359 nanoseconds.
stoi_test         completed in 9162406 nanoseconds.
stringstream_test completed in 9868536 nanoseconds.
stoi_test         completed in 9166439 nanoseconds.
stringstream_test completed in 9059762 nanoseconds.
stoi_test         completed in 11558076 nanoseconds.
stringstream_test completed in 11673084 nanoseconds.
stoi_test         completed in 13432386 nanoseconds.
...

正如您可能看到的,两种测试都给出了可比的结果,有时一种比另一种略好。我的工作站很忙(有一个后台进程完全占用了六个内核中的一两个),但即使在这种情况下,测试结果对我来说还是令人满意的。

关于c++ - 为什么 stoi 比没有 -O3 的 stringstream 慢得多?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35307176/

10-13 06:28