[comp.lang.perl] Backwards cat

tchrist@convex.COM (Tom Christiansen) (12/17/89)

In article <5360@omepd.UUCP> merlyn@iwarp.intel.com (Randal Schwartz) writes:
|In article <6488@lindy.Stanford.EDU>, odin@ucscb (Jon Granrose) writes:
|| I am looking for a program that will cat a file backwards.  Not with all the
|| letters reversed but one that prints the last line, then the second to last
|| line, 3 from the last line, etc.)  If it doesn't exist then I will write my
|| own but I thought I'd ask first.
|
|Don't write your own... get Perl...
|perl -e 'print reverse(<>);'
|Simple, eh?

My first reaction at the original posting was to use "tail -r", but I like
Randal's solution a good deal anyway.  You'd think a dedicated utility
like tail would be more efficient than a general purpose interpreted
language like perl (or awk, which is less general-purpose), but much to my
surprise, I found this (on a minimally configured CONVEX C1):

% wc /etc/termcap
    2235    8179  102598 /etc/termcap
% time tail -r /etc/termcap > /dev/null
1.4u 0.3s 0:01 93% 0+18k 0+0io 67pf+0w
% time perl -e 'print reverse(<>);' /etc/termcap > /dev/null
0.7u 0.3s 0:01 91% 0+25k 0+5io 127pf+0w

I also tried Jay Maynards awk solution:

% time awk '{ lines[NR] = $0 } END { for(i=NR; i>=1; i--) print lines[i]; }' < /etc/termcap > /dev/null
9.3u 0.9s 0:12 79% 0+3k 27+0io 95pf+0w
% time nawk ....
6.9u 0.8s 0:08 89% 0+4k 39+1io 114pf+0w
% time gawk ....
6.6u 1.9s 0:09 86% 0+4k 11+8io 214pf+0w


Which is pretty sad performance by my book.

--tom

    Tom Christiansen                       {uunet,uiucdcs,sun}!convex!tchrist 
    Convex Computer Corporation                            tchrist@convex.COM
		 "EMACS belongs in <sys/errno.h>: Editor too big!"