stdio performance in tango, stdlib, and perl

torhu fake at address.dude
Wed Mar 21 22:36:28 PDT 2007


James Dennett wrote:
<snip>
> Try the way IOStreams would be used if you didn't want
> it to go slowly:
> 
> #include <string>
> #include <iostream>
> 
> int main() {
>     std::ios_base::sync_with_stdio(false);
>     std::cin.tie(NULL);
>     std::string s;
>     while (std::getline(std::cin, s)) {
>         std::cout << s << '\n';
>     }
> }
<snip>


I did some tests with a 58 MB file, containing one million lines.  I'm 
on winxp.  I ran each test a few times, timing them with a stopwatch.  I 
threw in a naive C version, and a std.cstream version, just out of 
curiousity.

It seems that using cin.tie(NULL) doesn't matter with msvc 7.1, but with 
mingw it does.  Basically, Tango wins hands down on my system.  Whether 
the Tango version flushes after each line or not, doesn't seem to matter 
much on Windows.


Compiled with:
dmd -O -release -inline
gcc -O2  (mingw 3.4.2)
cl /O2 /GX


Fastest first:

tango.io.Console, no flushing (Andrei's): ca 1.5s

C, reusing buffer, gcc & msvc71: ca 3s

James' C++, gcc: 3.5s

Phobos std.cstream, reused buffer: 11s

C w/malloc and free each line, msvc71: 23s

Andrei's C++, gcc: 27s

C w/malloc and free each line, gcc: 37s

Andrei's C++, msvc71: 50s

James' C++,  msvc: 51s


---
// Tango
import tango.io.Console;

void main() {
   char[] line;

   while (Cin.nextLine(line)) {
     //Cout(line).newline;
     Cout(line)("\n");
   }
}
---

---
// Phobos std.cstream test
import std.cstream;

void main() {
    char[] buf = new char[1000];
    char[] line;
    while (!din.eof()) {
       line = din.readLine(buf);
       dout.writeLine(line);
    }
}
---

---
/* C, reusing buffer */
#include <stdio.h>
#include <stdlib.h>

char buf[1000];

int main() {
    while (fgets(buf, sizeof(buf), stdin)) {
       fputs(buf, stdout);
    }
    return 0;
}
---

---
/* C test w/malloc and free */
#include <stdio.h>
#include <stdlib.h>

int main() {

    char *buf = malloc(1000);
    while (fgets(buf, sizeof(buf), stdin)) {
       fputs(buf, stdout);
       free(buf);
       buf = malloc(1000);
    }
    free(buf);
    return 0;
---

---
// Andrei's
#include <string>
#include <iostream>

int main() {
    std::string s;
    while (getline(std::cin, s)) {
      std::cout << s << '\n';
    }

    return 0;
}
---

---
// James'
#include <string>
#include <iostream>

int main() {
     std::ios_base::sync_with_stdio(false);
     std::cin.tie(NULL);
     std::string s;
     while (std::getline(std::cin, s)) {
         std::cout << s << '\n';
     }
}
---



More information about the Digitalmars-d mailing list