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

            Bug ID: 63466
           Summary: sstream is very slow
           Product: gcc
           Version: 5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andi-gcc at firstfloor dot org

sstream is very slow. Comparing two simple programs that parse a stream with C
and with sstream. The sstream version is an order of magnitude slower.

gcc version 4.9.1 20140423 (prerelease) (GCC) 

# C++
% time ./a.out  < testfile 

real    0m0.893s
user    0m0.888s
sys    0m0.004s


# C
time ./tstream-c  < testfile 

real    0m0.032s
user    0m0.030s
sys    0m0.001s

Here's a profile.

    16.13%    a.out  libc-2.18.so         [.] _IO_getc                          
    10.39%    a.out  libc-2.18.so         [.] _IO_ungetc                        
     9.15%    a.out  libstdc++.so.6.0.20  [.] std::basic_istream<char,
std::char_traits<char> >& std::getline<char, std::char_traits<char>,
std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&,
std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, char)  
     7.87%    a.out  libstdc++.so.6.0.20  [.] __dynamic_cast                    
     4.99%    a.out  libc-2.18.so         [.] __GI___strcmp_ssse3               
     3.95%    a.out  libstdc++.so.6.0.20  [.] std::basic_istream<char,
std::char_traits<char> >& std::operator>><char, std::char_traits<char>,
std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&,
std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)        
     3.89%    a.out  libc-2.18.so         [.] _int_free                         
     2.79%    a.out  libstdc++.so.6.0.20  [.]
__cxxabiv1::__vmi_class_type_info::__do_dyncast(long,
__cxxabiv1::__class_type_info::__sub_kind, __cxxabiv1::__class_type_info
const*, void const*, __cxxabiv1::__class_type_info const*, void const*,
__cxxabiv1::__class_type_info::__dyncast_result&) const
     2.65%    a.out  a.out                [.] main                              
     2.58%    a.out  libc-2.18.so         [.] malloc                            
     2.30%    a.out  libstdc++.so.6.0.20  [.]
__cxxabiv1::__si_class_type_info::__do_dyncast(long,
__cxxabiv1::__class_type_info::__sub_kind, __cxxabiv1::__class_type_info
const*, void const*, __cxxabiv1::__class_type_info const*, void const*,
__cxxabiv1::__class_type_info::__dyncast_result&) const 
     1.96%    a.out  libc-2.18.so         [.] _int_malloc                       
     1.86%    a.out  libstdc++.so.6.0.20  [.]
std::istream::sentry::sentry(std::istream&, bool)                               
     1.55%    a.out  libc-2.18.so         [.] _IO_sputbackc                     
     1.51%    a.out  libstdc++.so.6.0.20  [.]
__gnu_cxx::stdio_sync_filebuf<char, std::char_traits<char> >::underflow()       

Test case:

Generate test file:

 perl -e 'for($i=0;$i<1000000;$i++) { printf("%d %d\n", $i, $i); } ' > testfile

C++ version:

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

using namespace std;

void __attribute__((noinline, noclone)) func(string &, string &)
{
}

int main()
{
        string line;
        while (getline(cin, line)) {
                istringstream iss(line);
                string index, s;

                if (!(iss >> index >> s))
                       continue;
                func(index, s);
        }
        return 0;
}

C version:

#define _GNU_SOURCE 1
#include <stdio.h>
#include <string.h>

void __attribute__((noinline, noclone)) func(char *a, char *b)
{
}

int main()
{
        char *line = NULL;
        size_t linelen = 0;
        while (getline(&line, &linelen, stdin) > 0) {
                char *p = line;
                char *a = strsep(&p, " \t\n");
                char *b = strsep(&p, " \t\n");
                func(a, b);
        }
        return 0;
}

Reply via email to