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

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The calls you see to getc are nothing to do with <sstream>, they're from the
std::getline call reading from stdin, and are required because you didn't tell
the C++ runtime that you don't need it to be synchronised with C stdio. If you
don't need to mix C and C++ I/O then you forgot the most important thing for
iostream performance on standard I/O streams:

  std::ios::sync_with_stdio(false);

If you don't need that synchronisation, don't use it.

Something like this is a more accurate equivalent of the C program, and much
closer in performance:

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

using namespace std;

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

int main()
{
  std::ios::sync_with_stdio(false);
  string line;
  while (getline(cin, line)) {
    char* a = &line[0];
    char* b = nullptr;
    auto pos1 = line.find_first_of(" \t\n");
    if (pos1 != std::string::npos) {
      line[pos1] = '\0';
      b = a + pos1 + 1;
      auto pos2 = line.find_first_of(" \t\n", pos1+1);
      if (pos2 != std::string::npos) {
        line[pos2] = '\0';
      }
    }
    func(a, b);
  }
}

Reply via email to