Skip to content

Instantly share code, notes, and snippets.

@yoshikazusawa
Last active August 29, 2015 13:59
Show Gist options
  • Save yoshikazusawa/10648578 to your computer and use it in GitHub Desktop.
Save yoshikazusawa/10648578 to your computer and use it in GitHub Desktop.
Perlでミリ秒単位の時間計測を行う(Time::HiResを利用) ref: http://qiita.com/yoshikazusawa/items/b275104ffe869fa76157
use strict;
use warnings;
use utf8;
use feature qw( say );
binmode(STDOUT, ":utf8");
use Time::HiRes qw( usleep gettimeofday tv_interval );
# 現在時刻を取得([currentsec, currentmicrosec]の構造になっている)
my $t0 = [gettimeofday];
# 200 * 1000マイクロ秒 → 200ミリ秒のsleep
usleep(200 * 1000);
# 浮動小数点数で経過時間を取得(このサンプルだと0.20xxxx秒程)
my $elapsed = tv_interval($t0);
say int($elapsed * 1000), "ミリ秒";
perl -MTime::HiRes=usleep,gettimeofday,tv_interval -E 'my $t0 = [gettimeofday]; usleep(200 * 1000); say int(tv_interval($t0) * 1000)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment