Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7175633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:20:25+00:00 2026-05-28T16:20:25+00:00

So I was trying to write myself a command for a linux pipeline. Think

  • 0

So I was trying to write myself a command for a linux pipeline. Think of it as a replica of gnu ‘cat’ or ‘sed’, that takes input from stdin, does some processing and writes to stdout.

I originally wrote an AWK script but wanted more performance so I used the following c++ code:

std::string crtLine;
crtLine.reserve(1000);
while (true)
{
    std::getline(std::cin, crtLine);
    if (!std::cin) // failbit (EOF immediately found) or badbit (I/O error)
        break;

    std::cout << crtLine << "\n";
}

This is exactly what cat (without any parameters does).
As it turns out, this program is about as slow as its awk counterpart, and nowhere near as fast as cat.

Testing on a 1GB file:

$time cat 'file' | cat | wc -l
real    0m0.771s

$time cat 'file' | filter-range.sh | wc -l
real    0m44.267s

Instead of getline(istream, string) I tried cin.getline(buffer, size) but no improvements. This is embarassing, is it a buffering issue? I also tried fetching 100KB at a time instead of just one line, no help! Any ideas?

EDIT:
What you folks say makes sense, BUT the culprit is not string building/copying and neither is scanning for newlines. (And neither is the size of the buffer). Take a look at these 2 programs:

char buf[200];
while (fgets(buf, 200, stdin))
    std::cout << buf;

$time cat 'file' | ./FilterRange > /dev/null
real    0m3.276s




char buf[200];
while (std::cin.getline(buf, 200))
    std::cout << buf << "\n";

$time cat 'file' | ./FilterRange > /dev/null
real    0m55.031s

Neither of them manipulate strings and both of them do newline scanning, however one is 17 times slower than the other. They differ only by the use of cin.
I think we can safely conclude that cin screws up the timing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:20:27+00:00Added an answer on May 28, 2026 at 4:20 pm

    This is exactly what cat (without any parameters does).

    Not really. This has exactly the same effect as /bin/cat, but it does not use the same method.

    /bin/cat looks more like this:

    while( (readSize = read(inFd, buffer, sizeof buffer)) > 0)
      write(outFd, buffer, readSize);
    

    Notice that /bin/cat does no processing on its input. It doesn’t build a std::string out of it, it doesn’t scan it for \n, it just does one system call after another.

    Your program, on the other hand, builds strings, make copies of them, scans for \n, etc, etc.

    This small, complete program runs 2-3 orders of magnitude slower than /bin/cat:

    #include <string>
    #include <iostream>
    
    int main (int ac, char **av) {
      std::string crtLine;
      crtLine.reserve(1000);
      while(std::getline(std::cin, crtLine)) {
        std::cout << crtLine << "\n";
      }
    }
    

    I timed it thus:

    $ time ./x < inputFile > /dev/null
    $ time /bin/cat < inputFile > /dev/null
    

    EDIT
    This program gets within 50% of the performance of /bin/cat:

    #include <string>
    #include <iostream>
    #include <vector>
    
    int main (int ac, char **av) {
      std::vector<char> v(4096);
      do {
        std::cin.read(&v[0], v.size());
        std::cout.write(&v[0], std::cin.gcount());
      } while(std::cin);
    }
    

    In short, if your requirement is to perform line-by-line analysis of the input, then you will have to pay some price to use formatted input. If, on the other hand, you need to perform byte-by-byte analysis, then you can use unformatted input and go faster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write myself a program that calculates a function taken from a
im trying to write an app that will display a list off lines from
Context I'm trying to write myself a script that would toggle my WLAN adapter
I could probably write this myself, but the specific way I'm trying to accomplish
I've been trying myself, and searching online, to write this regular expression but without
I'm trying to add a IsImage property that I wrote myself to the HttpPostedFile
Trying to write a PowerShell cmdlet that will mute the sound at start, unless
Trying to write a couple of functions that will encrypt or decrypt a file
I am trying to write a function that produces a single solution to an
I am trying to write a function that will insert data into a MySQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.