[MLton] [MLton-devel] sadly slow character I/O speed

Stephen Weeks MLton@mlton.org
Thu, 1 Jan 2004 19:44:45 -0800


> I  did  a  simple  test  of  character  I/O (TextIO.input1) in a simple state
> machine (so all tail-recursive function calls) translating stdin  to  stdout.
...
> The  moral  is  that my MLton code, even with my own I/O, runs almost 3 times
> slower than my C code.  Definitely quite unfortunate.

Henry, I am finally able to respond positively your message from
September!  My last checkin made output1 competitive with C.  I just
did some timings and input1 is already competitive (7.15s for MLton vs
5.55s for gcc -O2 to read in one billion chars from /dev/zero).  So,
hopefully your IO tests will now also be competitive with C.  If they
aren't please send mail with the code.

For completeness, here is the C and SML code that I used to test
input.

--------------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>

#undef getc
#define getc	getc_unlocked

int main (int argc, char **argv) {
	int c;
	int count;
	int i;
	FILE *ins;

	if (2 != argc) {
		fprintf (stderr, "usage\n");
		return -1;
	}
	count =	atoi (argv[1]);
	ins = fopen ("/dev/zero", "r");
	for (i = count; i > 0; --i)
		c = getc (ins);
	fclose (ins);
	fprintf (stderr, "%d\n", c);
	return 0;
}

--------------------------------------------------------------------------------

val count = 1000000000
open TextIO
val ins = openIn "/dev/zero"
fun loop (n, c) =
   if n = 0
      then c
   else loop (n - 1, valOf (input1 ins))
val c = loop (count, #"a")
val _ = closeIn ins
val _ = print (concat [Int.toString (Char.ord c), "\n"])

--------------------------------------------------------------------------------