=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
                             UNIX GURU UNIVERSE
                                UNIX HOT TIP

                       Unix Tip 2346 - June  4, 2006

                   
http://www.ugu.com/sui/ugu/show?tip.today
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

비효율 코드와 개선 코드

나는 사람들의 코드가 비효율적인 것을 볼때마다 진절머리가 난다.(※ 원작자의 생각임) 
여기에 가장 흔한 실수 3가지와 같은 작업을 하기 위한 좀 더 나은 방법을 적어본다.
 
비효율 코드: cat somefile | grep something
개선    코드: grep something somefile
이         유 : 두개의 프로그램(catgrep) 대신 하나의 프로그램(grep) 실행

비효율 코드: ps -ef | grep something | grep -v grep
개선    코드: ps -ef | grep [s]omething
이         유 : 세개의 명령(ps와 두개의 grep) 대신에 두개의 명령(psgrep)을 실행 
 
---------------------------------------------------------------------
(역자 註: grep에서 정규표현식을 지원하는데 []를 쓸 경우 grep -v grep
              효과를 내는 듯함.
 
다음은 GNU grep 2.5.1a 버전 이후부터 추가된 기능임. 상세사항은 manpage참조
        추가기능1 : -o 또는 --only-matching (only match)
        추가기능2 : --colo(u)r[=WHEN], WHEN=never, always, auto
                         이 옵션은 GREP_COLOR에 지정될 수 있음
        추가기능3 : -P(Perl 정규표현식 사용). 이 기능은 소스를 재컴파일해야함)
---------------------------------------------------------------------

비효율 코드: cat /dev/null > somefile
개선    코드: > somefile
이         유 : 한개의 명령(cat)과 입출력 redirection대신에 redirection만 실행

비록 비효율적인 방법이 같은 결과를 낼 지라도 개선된 방법이 훨씬 더 빠르다.
이 방법이 하찮게 보일지 모르지만 큰 파일이나 루프를 다룰 때 그  이득을 볼
수 있을 것이다.
 
@@@@@@@@@@@@@@@@@@@@[ 원문 ]@@@@@@@@@@@@@@@@@@@@
 
THE BAD AND THE GOOD

I cringe anytime I see someone code inefficiently.  Here are
three of the most common mistakes, followed by a better way to
do the same thing.

Bad:    cat somefile | grep something
Better: grep something somefile
Why:    You're running one program (grep) instead of two (cat
and grep).

Bad:    ps -ef | grep something | grep -v grep
Better: ps -ef | grep [s]omething
Why:    You’re running two commands (grep) instead of three (ps
and two greps).

Bad:    cat /dev/null > somefile
Better: > somefile
Why:    You're running a command (cat) with I/O redirection,
instead of just redirection.

Although the bad way will have the same result, the good way is
far faster.  This may seem trivial, but the benefits will really
show when dealing with large files or loops.
반응형
Posted by She쥐포s

블로그 이미지
She쥐포s
Yesterday
Today
Total

달력

 « |  » 2024.4
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

최근에 올라온 글

최근에 달린 댓글

글 보관함