[perl] regex VS split

spring·2020년 11월 9일
0

perl에서도 속도를 고려할 때가 있다.

정규표현식과 split….

use Time::HiRes qw/gettimeofday tv_interval/;
my $start = [gettimeofday];
@a=<>=~/(\d+)/g;
my $end = [gettimeofday];
print sprintf("%.6f s", tv_interval($start, $end));

위는 정규표현식으로 공백으로 구분된 숫자를 모두 읽는다.

test.txt 는 10만개의 정수

test2.txt는 100만개의 정수이다.

output
C:\Users\ehtki\Desktop\perl>perl test.pl < test.txt 
0.046141 s
C:\Users\ehtki\Desktop\perl>perl test.pl < test2.txt
2.617017 s

이제 split의 시간을 측정 해보자.

use Time::HiRes qw/gettimeofday tv_interval/;
my $start = [gettimeofday];
@a=split / /,<>;
my $end = [gettimeofday];
print sprintf("%.6f s", tv_interval($start, $end));
output
C:\Users\ehtki\Desktop\perl>perl test.pl < test.txt
0.013655 s
C:\Users\ehtki\Desktop\perl>perl test.pl < test2.txt
0.147275 s
ㄷㄷ split 쓰자
profile
Researcher & Developer @ NAVER Corp | Designer @ HONGIK Univ.

0개의 댓글

관련 채용 정보