[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Shell Golf
You could prune about 5 chars off by using an END block and the -n
switch, and get another two by removing the quotes around the array
(though the quotes give you prettier output). You get 2 more chars by
calling length with no args, since it defaults to $_. One more comes
from bumping the array up next to the print command. We're down to 64
if you put the command right next to the -e switch and use "warn"
instead of print. :)
perl -ne'$x=length;@n=($x,$ARGV,$_)if$x>$n[0];END{warn@n}' *.pl
I was trying to find a way to put the string into $@ so warn can be
called without args, but it's just not presenting itself (and I should
really be doing real work while I'm at work). "die" would be nice, but
it prints a mess if the string passed doesn't end with "\n"...
--Danny, dissapointed that split/./ is more chars than length, since
that's a far more entertaining way to find the length of $_ :)
Steven Pritchard wrote:
> On Wed, May 07, 2003 at 06:46:41PM +0000, mike808@users.sourceforge.net wrote:
>
>>My solution clocks in at a hefty *59* characters. Any contenders?
>>
>>perl -p -e '$_=length($_)." $ARGV $_"' *.pl|sort -n|tail -1
>
>
> Not as short, but a heck of a lot more efficient:
>
> perl -e 'while(<>){$x=length$_;@n=($x,$ARGV,$_)if$x>$n[0]}print"@n"' *.pl
>
> 1000 runs of the original:
>
> 8.25user 7.59system 0:16.40elapsed 96%CPU (0avgtext+0avgdata 0maxresident)k
> 0inputs+0outputs (567238major+175905minor)pagefaults 0swaps
>
> 1000 runs of my script:
>
> 5.24user 3.53system 0:09.61elapsed 91%CPU (0avgtext+0avgdata 0maxresident)k
> 0inputs+0outputs (316240major+71180minor)pagefaults 0swaps
>
> This is in a directory with 9 .pl files with a total of 355 lines, so
> the difference is *much* larger with more/bigger files. Here's a
> directory with 9 .c files with a total of 3868 lines:
>
> Original:
>
> 38.51user 11.17system 0:52.86elapsed 93%CPU (0avgtext+0avgdata 0maxresident)k
> 0inputs+0outputs (567238major+234930minor)pagefaults 0swaps
>
> Mine:
>
> 9.26user 3.87system 0:16.95elapsed 77%CPU (0avgtext+0avgdata 0maxresident)k
> 0inputs+0outputs (316240major+71195minor)pagefaults 0swaps
>
> And to get really crazy, I tried the same thing *once* on an old-ish
> copy of XFree86 that I happened to have lying around (only on *.h
> though, otherwise the argument list was too long ;):
>
> Original:
>
> 9.67user 1.78system 0:12.29elapsed 93%CPU (0avgtext+0avgdata 0maxresident)k
> 0inputs+0outputs (908major+9030minor)pagefaults 0swaps
>
> Mine:
>
> 1.11user 0.24system 0:01.51elapsed 89%CPU (0avgtext+0avgdata 0maxresident)k
> 0inputs+0outputs (655major+436minor)pagefaults 0swaps
>
> That's on 569644 lines in 2294 files.
>
> Steve
-
To unsubscribe, send email to majordomo@luci.org with
"unsubscribe luci-discuss" in the body.