Actually, the following has a bug:
#!/usr/bin/perl
while ($curr_line = <STDIN>) {
print scalar reverse($currr_line);
}
The output begins with a spurious \n. I rewrote it like this to fix it:
#! /usr/bin/perl
while (($l=<>))
{
chop $l;
print scalar reverse($l),"\n";
}
the re-written version executes about 5.5 times slower that the
original C code instead of 3. The original C code had a small bug - I
forgot to add exit(1) to die, but it did not matter if you did not try
it with an error condition. I have fixed it now.
Good things happen when the actual code is posted. I've worked with
Perl for years, but was not aware for the scalar reverse() trick. I
hope we can make this thread a learning experience for all.
I agree that Perl is the best language for this particular task for
files that are sufficiently small, but that was not the point. An
argument was made that with Java or Haskell you could beat C on
performance while significantly reducing the development time.
Barry's comment proves the point that Java is not really that fast for
development time. So far I've seen a lot of essays about how Java
can be faster, but not the code. I do not have a whole lot of time
myself, that is why I rarely post anything anymore, so I understand
that. But I know essays take time to write as well, and you guys seem
to be finding it. So does that mean that a good Java solution takes so
long to write that you'd rather write a few lengthy paragraphs about
how JIT can win the game than actually prove it with the code?
--
Sasha Pachev
Fast Running Blog.
http://fastrunningblog.com
Run. Blog. Improve. Repeat.
/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/