2006 Underhanded C Contest 3 Jul 2006 Entry by Tom Murphy VII http://tom7.org/ -- Instructions -- The file tom7.c compiles with gcc on both windows (cygwin) and linux: gcc -Wall tom7.c -o tom7 It also compiles with Microsoft Visual Studio: cl tom7.c -- Ridiculous performance degradation -- The program is much slower on x86 Linux than on x86 Windows. The following are average runtimes to compute wordcounts for book1* of the Calgary Corpus[1] on the same 1Ghz Pentium M machine. The program produces identical results, modulo line ending conventions, in each case: windows (gcc 3.4.4) 0.621s windows (Visual C++ 13.00.9466) 0.320s linux (gcc 4.0.3) 14.298s Linux is 23 to 44 times slower! On linux the slowdown is similar in gcc 3.2.3, 3.2.1, and as far back as egcs 2.91.66 as well. Windows is similarly fast with Visual C++ version 12.00.8804. * book1 contains a ^Z character, which visual C++ treats as end of file; I removed this character for these tests. -- Program features (contains hints/spoilers) -- - The program includes only three headers, each of which is completely standard: stdlib.h, stdio.h, and malloc.h. - The program is 100% safe legal C with no undefined behavior; it produces identical correct output in any compliant C implementation on any architecture. It does not overflow buffers nor rely on any peculiarities of memory layout. It does not use any conditional compilation. - The slowness is in the code supplied, not in any library function. - Larger input files create a larger disparity between the two platforms. (For book1 concatenated with itself four times, the linux version takes 1 minute 13 seconds, and the windows version less than one second.) For the solution, see the comments in tom7-spoilers.c. -- References -- [1] Calgary Corpus; http://corpus.canterbury.ac.nz/descriptions/#calgary