앞선 글에 적은 Perl의 수행 시간을 측정한 방법입니다.
CPAN에서 Time::HiRes 를 참조했는데요, microsecond 단위까지 측정이 가능합니다.
gettimeofday()를 이용하면 초단위와 microsecond 단위의 두개의 값을 배열 형태로 반환합니다. 그래서 아래와 같이 사용하시면 됩니다.
($ssec, $susec) = gettimeofday();
저의 경우는 다음과 같은 방법으로 작업 수행 시간을 측정했습니다.
use Time::HiRes qw(gettimeofday);
...
my $ssec;
my $esec;
my $susec;
my $eusec;
($ssec, $susec) = gettimeofday();
... Do Something ...
($esec, $eusec) = gettimeofday();
my $sec = ($esec - $ssec)*1000;
$sec += (($eusec - $susec)/1000);
...
my $ssec;
my $esec;
my $susec;
my $eusec;
($ssec, $susec) = gettimeofday();
... Do Something ...
($esec, $eusec) = gettimeofday();
my $sec = ($esec - $ssec)*1000;
$sec += (($eusec - $susec)/1000);
결과적으로 $sec에 millisecond 단위의 값이 저장되게 됩니다. Java에서 기본적으로 제공하는 시간 단위가 milisecond이기 때문에 단위를 통일하기 위해 위와 같이 처리하였습니다.
[추가]
JEEN 님게서 좋은 정보를 주셔서 CPAN에서 Benchmark 에 대해 찾아보았습니다.
CPAN에 보니 아래와 같은 예제가 있습니다.
use Benchmark;
$t0 = new Benchmark;
# ... your code here ...
$t1 = new Benchmark;
$td = timediff($t1, $t0);
print "the code took:",timestr($td),"\n";
$t0 = new Benchmark;
# ... your code here ...
$t1 = new Benchmark;
$td = timediff($t1, $t0);
print "the code took:",timestr($td),"\n";
제가 작성한 코드에 해당 부분을 추가해보았습니다.
use Benchmark ':hireswallclock';
...
($ssec, $susec) = gettimeofday();
my $t0 = new Benchmark;
... Do Something....
($esec, $eusec) = gettimeofday();
my $t1 = new Benchmark;
my $td = timediff($t1, $t0);
print "Benchmark Time: ", timestr($td), "\n";
my $sec = ($esec - $ssec)*1000;
$sec += (($eusec - $susec)/1000);
...
...
($ssec, $susec) = gettimeofday();
my $t0 = new Benchmark;
... Do Something....
($esec, $eusec) = gettimeofday();
my $t1 = new Benchmark;
my $td = timediff($t1, $t0);
print "Benchmark Time: ", timestr($td), "\n";
my $sec = ($esec - $ssec)*1000;
$sec += (($eusec - $susec)/1000);
...
이에 대한 실행 결과는 다음과 같이 나옵니다.
Benchmark Time: 1.98443 wallclock secs ( 0.61 usr + 1.25 sys = 1.86 CPU)
이전의 Time:HiRes 를 이용한 결과는
Elapsed Time: 1984.425 msec
입니다. 두 결과가 거의 일치하는 것을 알 수 있습니다.
그리고 제 코드에보면 use Benchmark ':hireswallclock'; 로 선언한 것을 알 수 있는데, Time:HiRes 가 시스템에 설치되어 있으면 시간 단위를 microsecond 까지 지원해줍니다.
댓글을 달아 주세요
Benchmark 모듈도 도움이 될겁니다.
2008/12/18 11:23좋은 정보 감사합니다.
2008/12/18 13:13