LoginSignup
3
0

More than 5 years have passed since last update.

Perlのforとwhileとmapの処理速度を比較してみた。

Last updated at Posted at 2018-04-29

概要

Perlのforとwhileとmapの処理速度をそれぞれ比較してみました。

環境

OS: macOS Sierra
プロセッサ: 2.3 GHz Intel Core i5
メモリ: 8 GB 2133 MHz LPDDR3

検証方法

それぞれ50万回ループさせて、Time::HiResで時間を測ってみる。

検証

test.pl
use strict;
use warnings;
use utf8;

use feature qw/say/;
use Time::HiRes qw( usleep gettimeofday tv_interval );

# for
my $t1 = [gettimeofday];
my $a = 0;
for (1..500000) {
    $a++;
}
say int(tv_interval($t1) * 1000), "ミリ秒";

# while
my $t2 = [gettimeofday];
my $b = 0;
while ($b < 500000) {
    $b++;
}
say int(tv_interval($t2) * 1000), "ミリ秒";

# map
my $t3 = [gettimeofday];
my $c = 0;
map {
    $c++
} (1..500000);
say int(tv_interval($t3) * 1000), "ミリ秒";

結果

$ perl test.pl
15ミリ秒
14ミリ秒
29ミリ秒

気にするほどではないが、for, whileに比べて、mapは少し遅いみたい

3
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0