Skip to content

Instantly share code, notes, and snippets.

@erikfrey
Created August 23, 2011 06:18
Show Gist options
  • Select an option

  • Save erikfrey/1164469 to your computer and use it in GitHub Desktop.

Select an option

Save erikfrey/1164469 to your computer and use it in GitHub Desktop.
python, c++, node on stdio
erik@roastbeef:~/proj$ cat child.py
import sys
line = sys.stdin.readline()
while line:
line = sys.stdin.readline()
erik@roastbeef:~/proj$ time python child.py < bigfile > derp
real 0m0.073s
user 0m0.056s
sys 0m0.008s
erik@roastbeef:~/proj$ cat child.cpp
#include <iostream>
int main(int argc, char * argv[])
{
std::ios::sync_with_stdio(false);
std::string line;
while (std::getline(std::cin, line))
std::cout << line << '\n';
}
erik@roastbeef:~/proj$ time ./child < bigfile > derp
real 0m0.366s
user 0m0.076s
sys 0m0.280s
erik@roastbeef:~/proj$ cat child.js
var stdin = process.openStdin();
var buffer = "";
stdin.on("data", function(chunk) {
buffer += chunk;
var beg = 0, end = 0;
while ((end = buffer.indexOf('\n', beg)) >= 0) {
process.stdout.write(buffer.substr(beg, 1 + end - beg));
beg = end + 1;
}
buffer = buffer.substr(beg);
});
stdin.on("end", function(data) {
process.stdout.write(buffer);
});
erik@roastbeef:~/proj$ time node child.js < bigfile > derp
real 0m6.307s
user 0m4.208s
sys 0m1.996s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment