https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91636

            Bug ID: 91636
           Summary: performance regression about const string optimization
           Product: gcc
           Version: 7.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: baiwfg2 at gmail dot com
  Target Milestone: ---

Hi. I'm benchmarking const std::string optimization with different gcc version
and find that gcc 7.3.1 is not working well. Here's my benchmark code (I also
put it on wandbox: https://wandbox.org/permlink/vuAZWRp4H6S6Foh2) :
```
#include <iostream>
#include <chrono>
#include <cstdlib>
#include <set>
#include <string>

using namespace std;


void bad() {
    for (int i=0; i<10000000; i++) {
        string s;
        s = "loooooooooooooooooooooooooooooooooooooooooooooooooog";
    }

}

void good() {
    for (int i=0; i<10000000; i++) {
        const string s =
"loooooooooooooooooooooooooooooooooooooooooooooooooog";
    }

}

typedef void(*fp)();
void timing(fp fn) {
    auto begin = std::chrono::high_resolution_clock::now();
    fn();
    auto end = std::chrono::high_resolution_clock::now();
    std::cout <<
std::chrono::duration_cast<std::chrono::milliseconds>(end-begin).count() <<
std::endl;
}

int main(int argc, char* argv[])
{
    if (argc != 2) {
        cout << "wrong args\n";
        exit(1);
    }

    char c = argv[1][0];
    if (c == '1')
        timing(bad);
    else if (c == '2')
        timing(good);
}
```

Obviously the good() is better than bad(). But when I try compiling:

g++-7.3.1 a.cc -O2

And run it. It gives much worse result(good() is -7% more efficient than bad())
than any other version does, such as gcc 7.3.0, 7.4.0, 8.3.0( 30-40% more
efficient).

My gcc 7.3.1 version info is:

[root@VM_11_190_centos /data1/ethencao]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,objc,obj-c++,fortran,ada,go,lto --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--with-default-libstdcxx-abi=gcc4-compatible --enable-gnu-unique-object
--enable-linker-build-id --with-gcc-major-version-only
--with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl
--enable-libmpx --enable-offload-targets=nvptx-none --without-cuda-driver
--enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64
--build=x86_64-redhat-linux
Thread model: posix
gcc version 7.3.1 20180303 (Red Hat 7.3.1-6) (GCC) 

By the way, I don't install gcc by source compiling. Instead I use yum,
apt-get, or docker to use it.

Reply via email to