stdio performance in tango, stdlib, and perl

Andrei Alexandrescu (See Website For Email) SeeWebsiteForEmail at erdani.org
Wed Mar 21 14:36:10 PDT 2007


I've ran a couple of simple tests comparing Perl, D's stdlib (the coming 
release), and Tango.

First, I realize I should make an account on dsource.org and post the 
following there, but I'll mention here that it's quite disappointing 
that Tango's idiomatic method of reading a line from the console 
(Cin.nextLine(line) unless I missed something) chose to chop the newline 
automatically. The Perl book spends half a page or so explaining why 
it's _good_ that the newline is included in the line, and I've been 
thankful for that on numerous occasions when writing Perl. Please put 
the newline back in the line.

Anyhow, here's the code. The D up-and-coming stdio version:

import std.stdio;
void main() {
   char[] line;
   while (readln(line)) {
     write(line);
   }
}

The Tango version:

import tango.io.Console;
void main() {
   char[] line;
   while (Cin.nextLine(line)) {
     Cout(line).newline;
   }
}

(The .newline adds back the information that nextLine promptly lost, 
sigh.) I'm not sure whether this is the idiomatic way of reading and 
writing lines in Tango, but tango.io.Stdout seems to say so: "If you 
don't need formatted output or unicode translation, consider using the 
module tango.io.Console directly." - which suggests that Console would 
be the most primitive stdio library.

The Perl version:
#!/usr/bin/env perl
while (<>) {
   print;
}

All programs operate in the same exact boring way: read a line from 
stdin, print it, lather, rinse, repeat.

I passed a 31 MB text file (containing a dictionary that I'm using in my 
research) through each of the programs above. The output was set to 
/dev/null. I've ran the same program multiple times before the actual 
test, so everything is cached and the process becomes 
computationally-bound. Here are the results summed for 10 consecutive 
runs (averaged over 5 epochs):

13.9s		Tango
6.6s		Perl
5.0s		std.stdio


Andrei



More information about the Digitalmars-d mailing list