home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / shell / 3177 < prev    next >
Encoding:
Text File  |  1992-07-29  |  2.0 KB  |  55 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!sun-barr!cs.utexas.edu!convex!tchrist
  3. From: Tom Christiansen <tchrist@convex.COM>
  4. Subject: Re: Sort problem . .
  5. Message-ID: <1992Jul29.132526.2223@convex.com>
  6. Originator: tchrist@convex.convex.com
  7. Sender: usenet@convex.com (news access account)
  8. Nntp-Posting-Host: convex.convex.com
  9. Reply-To: tchrist@convex.COM (Tom Christiansen)
  10. Organization: CONVEX Realtime Development, Colorado Springs, CO
  11. References: <1992Jul28.095415.4640@prl.dec.com> <1992Jul28.102938.12375@news.eng.convex.com> <1992Jul29.044600.27445@cerberus.bhpese.oz.au>
  12. Date: Wed, 29 Jul 1992 13:25:26 GMT
  13. X-Disclaimer: This message was written by a user at CONVEX Computer
  14.               Corp. The opinions expressed are those of the user and
  15.               not necessarily those of CONVEX.
  16. Lines: 37
  17.  
  18. From the keyboard of Sm@cerberus.bhpese.oz.au (Scott Merrilees):
  19. :Tom Christiansen <tchrist@convex.COM> writes:
  20. :>    ps axu | (read line; echo "$line"; sort +3rn) 
  21. :
  22. :ps axu | sed 1d | sort -nr +3
  23.  
  24. I was trying to avoid sending the data stream through yet another
  25. process in the chain.  But apparently starting a subshell was too much
  26. trouble:
  27.  
  28. $ time sh -c 'ps axu | (read line; echo "$line"; sort +3rn)' > /dev/null
  29.     1.508u 1.635s 0:01.89 50.0% 0+0k 46+3io 113pf+0w
  30.  
  31. $ time sh -c "ps axu | sed 1d | sort -nr +3" > /dev/null
  32.     1.482u 1.534s 0:01.81 50.0% 0+0k 32+5io 106pf+0w
  33.  
  34. On the other hand, a bit to my surprise, this
  35.  
  36.     #!/usr/bin/perl
  37.     open(PS, "ps axu |") || die "cannot fork: $!";
  38.     print scalar(<PS>);
  39.     while (<PS>) { push(@lines, $_); push(@idx, (split(' '))[3]); }
  40.     print @lines [ sort { $idx[$b] <=> $idx[$a]; } 0..$#idx ];
  41.  
  42. runs in this time:
  43.  
  44.     0.779u 0.107s 0:00.53 50.0% 0+0k 3+0io 82pf+0w
  45.  
  46. Which is better than either of the pure shell solutions.
  47.  
  48. --tom
  49.  
  50. -- 
  51. "GUIs normally make it simple to accomplish simple actions and impossible
  52. to accomplish complex actions."   --Doug Gwyn  (22/Jun/91 in comp.unix.wizards)
  53.  
  54.      Tom Christiansen           tchrist@convex.com      convex!tchrist
  55.