Don Stewart wrote:
andrewcoppin:
(Apparently the library isn't 100% complete as yet.)

Right, we haven't bothered with streaming versions of 'words'. However, the maximumBy and map happily fuse into a single loop.

Indeed. And hopefully when words gets implemented, we will have One Loop to rule them all... er... well you know what I mean.

Hmm, perhaps to really show this off, we need a more complicated program. Something that would be just too hard to implement as 1 loop in C, but is easy for the GHC optimiser to build. I shall meditate on this further...

Do you have the single loop C program, btw? I'd be curious to see if
this is really "feasible". It would have to do the buffering, tokenising
and accumulating in one go. I'd imagine it is a bit hairy.

And, it should not significantly outperform, say, a bytestring version.
If it does, I'd like to see that.

First version:

n = 0;
while( n < FILE_SIZE )
{
while( n < FILE_SIZE  && file[n++] == ' ' ); wStart = n;
while( n < FILE_SIZE  && file[n++] != ' ' ); wLength = n - wStart;
if( wLength > strlen( longestString ) ) strncpy( longestString , file + wStart , wLength );
}

Takes 0.016 seconds to process a 2.4 MB file. [But not the same one Don used.]

Then Mr C++ looked at it and said "OMG! You don't *never* use strlen() inside a loop!" and the second version was writting:

file[FILE_SIZE] = ' ';
n = 0;
maxLength = 0;
while( n < FILE_SIZE )
{
while( file[n++] == ' ' ); wStart = n;
while( file[n++] != ' ' ); wLength = n - wStart;
if( wLength > maxLength )
{
  longestWordStart = wStart;
  maxLength = wLength;
}
}
strncpy( longestString , file + longestWordStart , maxLength );

This version takes 0.005 seconds.

I have no idea what kind of hardware this is running on - or even which compiler or OS.

_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to