Introduction Languages Documentation News Implementations Scorecard Recent Changes Mailing List Wiki Alioth

Reverse A File

[cpu minus startup time]
    [sort] [sort]  
Source Code CPU (sec) Mem (KB) Lines Code Log
bash0.067014641log out
gcc0.0810406459log out
ocaml0.0970300823log out
mlton0.1280581656log out
oberon20.1510352029log out
clean0.27601999614log out
smlnj0.2830758860log out
gcj0.30101412829log out
python0.3869131606log out
psyco0.3879130808log out
perl0.4519200202log out
kaffe0.45391422429log out
se0.48391289226log out
java0.49591582829log out
mercury0.56591174418log out
pike0.579971961log out
g++0.6359325666log out
bigloo0.6469110404log out
ruby0.8039469921log out
ocamlb0.8239321623log out
lua0.853974289log out
gst0.89291002022log out
slang0.954933761log out
newlisp1.0028138687log out
cmucl1.09581192027log out
sablevm1.14681101229log out
mawk1.1808161764log out
fpascal1.2828669619log out
sbcl1.29681612427log out
tcl1.3558173166log out
gij1.47781860829log out
rep1.7447968814log out
felix1.82772014817log out
icon1.851777366log out
gawk1.8537267764log out
gwydion2.72462766814log out
chicken2.8876219088log out
xemacs3.0935533484log out
nice3.26353218412log out
php3.27153998010log out
guile3.8224128805log out
mzc4.24542721614log out
ghc5.2072631241log out
erlang6.32203756811log out
hipe6.52603605611log out
mzscheme6.83806412014log out
nhc9819.94801567801log out

Languages that compile to native code are in Bold Italics.


[Note: Values have been normalized to fall in the range of 0-10 for aesthetic reasons. Original value ranges are included on the X-axis.]

Click here for more detailed data and graphs.

[Results last updated: Sun Oct 17 23:01:18 2004 CDT]


About this test

For this test, each program should be implemented to do the same thing, following the guidelines below:

This test reverses the input file from stdin, line by line. This is like the tail test in Timing Trials, or, the Trials of Timing: Experiments with Scripting and User-Interface Languages by Brian W. Kernighan and Christopher J. Van Wyk.

In this test, it's okay to use input methods other than line-oriented I/O, such as reading the entire input from stdin, as long as each read is no more than 4096 bytes at a time.

Input file (100K, it is repeated N times). Output file (500K, for N=5).

For this test, solutions can assume that the file ends with a trailing newline.

Observations

Perl and Ruby are ideally suited for this little test, and they both implement a one-line solution.

The original Perl program from the Kernighan and Wyk study is sub-optimal. Ben Tilly suggested a one-liner that is very concise and noticeably faster.

Alternates

This section is for displaying alternate solutions that are either slower than ones above or perhaps don't quite meet my criteria for the competition, but are otherwise worthy of comment.

  • If we were reading from a file instead of standard input, we could use mmap, as shown in this C++ example, which uses Bill Lear's mmap class.
  • Here are some alternate solutions in OCaml: pattern matching array (faster still).
  • It was significantly slower to iterate over the list of lines in Python, than it was to use the map() function (here), or the current Python submission, which is faster still.
  • Here's another version using ( SmallEiffel), which uses an array of strings rather than the current linked list, and is noticeably slower.