Time-HiRes-1.55 > Time::HiRes
Time-HiRes-1.55
Other versions:
Time-HiRes-1.9719

名前

Time::HiRes - High resolution alarm, sleep, gettimeofday, interval timers

Time::HiRes - 高精度のalarm, sleep, gettimeofday, インターバル・タイマー

概要

  use Time::HiRes qw( usleep ualarm gettimeofday tv_interval );

  usleep ($microseconds);

  ualarm ($microseconds);
  ualarm ($microseconds, $interval_microseconds);

  $t0 = [gettimeofday];
  ($seconds, $microseconds) = gettimeofday;

  $elapsed = tv_interval ( $t0, [$seconds, $microseconds]);
  $elapsed = tv_interval ( $t0, [gettimeofday]);
  $elapsed = tv_interval ( $t0 );

  use Time::HiRes qw ( time alarm sleep );

  $now_fractions = time;
  sleep ($floating_seconds);
  alarm ($floating_seconds);
  alarm ($floating_seconds, $floating_interval);

  use Time::HiRes qw( setitimer getitimer
                      ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF ITIMER_REALPROF );

  setitimer ($which, $floating_seconds, $floating_interval );
  getitimer ($which);

説明

The Time::HiRes module implements a Perl interface to the usleep, ualarm, gettimeofday, and setitimer/getitimer system calls, in other words, high resolution time and timers. See the "EXAMPLES" section below and the test scripts for usage; see your system documentation for the description of the underlying nanosleep or usleep, ualarm, gettimeofday, and setitimer/getitimer calls.

Time::HiRes モジュールはusleep,ualarm, gettimeofday, そして setitimer/getitimer システムコーム、言い換えれば高精度の時刻と タイマーへのへのPerlインターフェースを実装します。 使い方については下記の"使用例"セクションとテスト・スクリプトをご覧ください; 基になっているnanosleep あるいは usleep, ualarm, gettimeofday, そして setitimer/getitimerについては、あなたのシステムのドキュメントを ご覧ください。

If your system lacks gettimeofday() or an emulation of it you don't get gettimeofday() or the one-argument form of tv_interval(). If your system lacks all of nanosleep(), usleep(), and select(), you don't get Time::HiRes::usleep() or Time::HiRes::sleep(). If your system lacks both ualarm() and setitimer() you don't get Time::HiRes::ualarm() or Time::HiRes::alarm().

あなたのシステムにgettimeofday()やそのエミュレーションがなければ、 gettimeofday()や引数1つの形でのtv_interval()は使えません。 あなたのシステムにnanosleep(), usleep(), そして select(),の全てが なければ、Time::HiRes::usleep()Time::HiRes::sleep()は使えません。 あなたのシステムにualarm() and setitimer() の両方がなければ Time::HiRes::ualarm()Time::HiRes::alarm()が使えません。

If you try to import an unimplemented function in the use statement it will fail at compile time.

useステートメントのなかで実装されていない関数をインポートしようとすれば、 コンパイル時に失敗します。

If your subsecond sleeping is implemented with nanosleep() instead of usleep(), you can mix subsecond sleeping with signals since nanosleep() does not use signals. This, however is unportable, and you should first check for the truth value of &Time::HiRes::d_nanosleep to see whether you have nanosleep, and then carefully read your nanosleep() C API documentation for any peculiarities. (There is no separate interface to call nanosleep(); just use Time::HiRes::sleep() or Time::HiRes::usleep() with small enough values.)

あなたの1秒以下のsleepがusleep()ではなくnanosleep()で実装されていれば、 nanosleep()はシグナルを使わないので、1秒以下のsleepとシグナルを混在させる ことができます。しかしこれは移植性を欠きます。そしてまず最初にnanosleepを 持っているのか、&Time::HiRes::d_nanosleepの値がtrueであるかチェック しなければなりません。それから特有なことがないか慎重にnanosleep() C API ドキュメントを読まなければなりません。(nanosleep()を呼び出す別の インターフェースはありません;十分に小さな値で単にTime::HiRes::sleep()Time::HiRes::usleep()を呼ぶだけです。)

Unless using nanosleep for mixing sleeping with signals, give some thought to whether Perl is the tool you should be using for work requiring nanosecond accuracies.

sleepとシグナルを混在させるためにnanosleepを使わなければ、 Perlがナノ秒の正確さを要求される仕事のために使うべきツールであるか という疑問があります。

The following functions can be imported from this module. No functions are exported by default.

このモジュールから以下の関数をインポートすることができます。 デフォルトでエクスポートされる関数はありません。

gettimeofday ()

In array context returns a two-element array with the seconds and microseconds since the epoch. In scalar context returns floating seconds like Time::HiRes::time() (see below).

配列コンテキストではエポックからの秒とマイクロ秒2つの要素の配列を返します。 スカラー・コンテキストでは、Time::HiRes::time()のように秒を 浮動小数点数で返します(下記参照)。

usleep ( $useconds )

Sleeps for the number of microseconds specified. Returns the number of microseconds actually slept. Can sleep for more than one second, unlike the usleep system call. See also Time::HiRes::sleep() below.

指定されたマイクロ秒数、sleepします。実際にsleepしたマイクロ秒数を 返します。usleepシステム・コールとは違い、1秒以上sleepすることができます。 下記のTime::HiRes::sleep()もご覧ください。

ualarm ( $useconds [, $interval_useconds ] )

Issues a ualarm call; the $interval_useconds is optional and will be zero if unspecified, resulting in alarm-like behaviour.

ualarmコールを発行します;$interval_usecondsはオプションで、 指定されなければ0になり、その結果はalarmのような動きをします。

tv_interval

tv_interval ( $ref_to_gettimeofday [, $ref_to_later_gettimeofday] )

Returns the floating seconds between the two times, which should have been returned by gettimeofday(). If the second argument is omitted, then the current time is used.

2つの時刻の時間を浮動小数点の秒数を返します。2つの時刻はgettimeofday()で 返されたものでなければなりません。2番目の引数が省略されると、 現在の時刻が使われます。

time ()

Returns a floating seconds since the epoch. This function can be imported, resulting in a nice drop-in replacement for the time provided with core Perl; see the "EXAMPLES" below.

浮動小数点でエポックからの秒数を返します。この関数はインポートする ことができます、その結果、Perlのコアで提供される差し込み式にtimeを 置き換えることになります;下記の"使用例"をご覧ください。

NOTE 1: This higher resolution timer can return values either less or more than the core time(), depending on whether your platform rounds the higher resolution timer values up, down, or to the nearest second to get the core time(), but naturally the difference should be never more than half a second.

注意 1: この高精度のtimeは、あなたのプラットホームがコアのtime()を 得るために、より高精度の時刻を切り上げたり、切り捨てたり、一番近い秒に するのいずれかによって、コアのtime()よりも多かったり、少なかったり することがあります。しかし当然、その差は0.5秒を越えることはありえません。

NOTE 2: Since Sunday, September 9th, 2001 at 01:46:40 AM GMT, when the time() seconds since epoch rolled over to 1_000_000_000, the default floating point format of Perl and the seconds since epoch have conspired to produce an apparent bug: if you print the value of Time::HiRes::time() you seem to be getting only five decimals, not six as promised (microseconds). Not to worry, the microseconds are there (assuming your platform supports such granularity in first place). What is going on is that the default floating point format of Perl only outputs 15 digits. In this case that means ten digits before the decimal separator and five after. To see the microseconds you can use either printf/sprintf with "%.6f", or the gettimeofday() function in list context, which will give you the seconds and microseconds as two separate values.

注意 2: 2001年9月9日 日曜日 GMTの午前 1:46:40より、 エポックからのtime()秒は、1_000_000_000を超えています。 Perlでのデフォルトの浮動小数点のフォーマットとエポックからの秒数が あいまって明らかなバグを作り出します:Time::HiRes::time()の値を 出力すると、保証された6桁(マイクロ秒)ではなく5桁の数字しか得ることが できません。ご心配なく。マイクロ秒は存在します(もちろんあなたの プラットホームが、そのような粒度をサポートしているものと想定しています)。 何が起きているかといえば、Perlの浮動小数点のデフォルトのフォーマットでは 15桁の数字しか出力しません。この場合、小数点の前に0桁の数字があり、 その後ろに5桁あります。マイクロ秒を見るためには"%.6f"printf/sprintfの どちらかを使うか、リスト・コンテキストでgettimeofday()を使うことができます。 この場合には、秒とマイクロ秒の2つを別の値として与えてくれます。

sleep ( $floating_seconds )

Sleeps for the specified amount of seconds. Returns the number of seconds actually slept (a floating point value). This function can be imported, resulting in a nice drop-in replacement for the sleep provided with perl, see the "EXAMPLES" below.

指定された秒数、sleepします。実際にsleepした秒数を(浮動小数点の値で) 返します。この関数はインポートする ことができます、その結果、Perlのコアで提供される差し込み式にsleepを 置き換えることになります;下記の"使用例"をご覧ください。

alarm ( $floating_seconds [, $interval_floating_seconds ] )

The SIGALRM signal is sent after the specified number of seconds. Implemented using ualarm(). The $interval_floating_seconds argument is optional and will be zero if unspecified, resulting in alarm()-like behaviour. This function can be imported, resulting in a nice drop-in replacement for the alarm provided with perl, see the "EXAMPLES" below.

指定され秒数後、SIGALRMシグナルが送られます。ualarm()を使って 実装されています。$interval_floating_seconds引数は オプションで指定されないと0になります。その結果、alarm()のような 動きになります。この関数はインポートする ことができます。その結果、Perlのコアで提供される差し込み式にalarmを 置き換えることになります;下記の"使用例"をご覧ください。

NOTE 1: With some operating system and Perl release combinations SIGALRM restarts select(), instead of interuping it. This means that an alarm() followed by a select() may together take the sum of the times specified for the the alarm() and the select(), not just the time of the alarm().

注意 1: オペレーティング・システムとPerlのリリースの 組み合わせによってはSIGALRMが中断させるのではなく、select()を 再開始させることもあります。これはalarm()の後にselect()を すると、alarm()に指定した時間通りにではなく、 alarm()select()に指定された時間の合計が掛かるかも しれないことを意味します。

setitimer ( $which, $floating_seconds [, $interval_floating_seconds ] )

Start up an interval timer: after a certain time, a signal arrives, and more signals may keep arriving at certain intervals. To disable a timer, use $floating_seconds of zero. If the $interval_floating_seconds is set to zero (or unspecified), the timer is disabled after the next delivered signal.

インターバル・タイマーを開始させます;一定の時間後、シグナルが届き、 ある間隔でシグナルが届き続けるかもしれません。タイマーを止めるには、 $floating_secondsを0にしてください。$interval_floating_secondsに 0が指定されると(あるいは指定されないと)、タイマーは次にシグナルが届いた 後に止められます。

Use of interval timers may interfere with alarm(), sleep(), and usleep(). In standard-speak the "interaction is unspecified", which means that anything may happen: it may work, it may not.

インターバル・タイマーを使うことはalarm(), sleep()usleep()に よって邪魔されるかもしれません。標準的には「相互作用は特定されていません」 つまりは何が起きるかわかりません:動くかもしれませんし、そうでないかもしれません。

In scalar context, the remaining time in the timer is returned.

スカラー・コンテキストではタイマーでの残り時間が返されます。

In list context, both the remaining time and the interval are returned.

リスト・コンテキストでは、残り時間とインターバルが返されます。

There are usually three or four interval timers available: the $which can be ITIMER_REAL, ITIMER_VIRTUAL, ITIMER_PROF, or ITIMER_REALPROF. Note that which ones are available depends: true UNIX platforms usually have the first three, but (for example) Win32 and Cygwin have only ITIMER_REAL, and only Solaris seems to have ITIMER_REALPROF (which is used to profile multithreaded programs).

通常3つあるいは4つのインターバル・タイマーが利用可能です:$whichITIMER_REAL, ITIMER_VIRTUAL, ITIMER_PROF, あるいは ITIMER_REALPROFに することができます。どれが利用できるかは以下のことに依存します:本当のUNIX プラットホームは通常最初の3つを持っています。しかし(例えば)Win32とCygwinは ITIMER_REALしかもっていません。SolarisはITIMER_REALPROFを持っているようです (これはマルチスレッドなプログラムをプロファイルするために使われます)。

ITIMER_REAL results in alarm()-like behavior. Time is counted in real time; that is, wallclock time. SIGALRM is delivered when the timer expires.

ITIMER_REALは結果としてalarm()のような動きにします。 時間は実時間;つまり壁時計の時間で計られます。 タイマーが時間切れになるとSIGALRMが送られます。

ITIMER_VIRTUAL counts time in (process) virtual time; that is, only when the process is running. In multiprocessor/user/CPU systems this may be more or less than real or wallclock time. (This time is also known as the user time.) SIGVTALRM is delivered when the timer expires.

ITIMER_VIRTUALは(プロセス)仮想の時間で時間を計ります; つまりプロセスが実行しているときだけです。マルチプロセッサ/ユーザ/CPU の システムではこれは実時間あるいは壁時計の時間とは多かったり、少なかったりします。 (この時間はユーザー時間(user time)とも呼ばれます。タイマーが 時間切れになるとSIGVTALRMが送られます。

ITIMER_PROF counts time when either the process virtual time or when the operating system is running on behalf of the process (such as I/O). (This time is also known as the system time.) (The sum of user time and system time is known as the CPU time.) SIGPROF is delivered when the timer expires. SIGPROF can interrupt system calls.

ITIMER_PROF はプロセス仮想時間、あるいは(I/Oのように)プロセスの半分で オペレーティング・システムが走っている時間を計ります。 (この時間はシステム時間(system time)とも呼ばれます。) タイマーが時間切れになると、SIGPROFが送られます。SIGPROFは システム・コールを中断させることができます。

The semantics of interval timers for multithreaded programs are system-specific, and some systems may support additional interval timers. See your setitimer() documentation.

マルチスレッドなプログラムでのインターバル・タイマーの意味は システムによります。そしてシステムによっては、さらに他の インターバル・タイマーをサポートしているかもしれません。 あなたのsetitimer()のドキュメントをご覧ください。

getitimer ( $which )

Return the remaining time in the interval timer specified by $which.

$whichで指定されたインターバル・タイマーでの残り時間を返します。

In scalar context, the remaining time is returned.

スカラー・コンテキストでは、残り時間が返されます。

In list context, both the remaining time and the interval are returned. The interval is always what you put in using setitimer().

リスト・コンテキストでは残り時間とインターバルの両方が返されます。 インターバルは常に、setitimer()を使ったときに入れたものです。

  use Time::HiRes qw(usleep ualarm gettimeofday tv_interval);

  $microseconds = 750_000;
  usleep $microseconds;
  # signal alarm in 2.5s & every .1s thereafter
  ualarm 2_500_000, 100_000;    
  # 2.5秒後、その後は0.1行ごとにシグナル・アラーム
  ualarm 2_500_000, 100_000;    
  # get seconds and microseconds since the epoch
  ($s, $usec) = gettimeofday;
  # エポックからの秒とマイクロ秒を取得
  ($s, $usec) = gettimeofday;
  # measure elapsed time 
  # (could also do by subtracting 2 gettimeofday return values)
  $t0 = [gettimeofday];
  # do bunch of stuff here
  $t1 = [gettimeofday];
  # do more stuff here
  $t0_t1 = tv_interval $t0, $t1;
  # 経過時間の計測
  # (2つのgettimeofdayの戻り値を引くことによっても可能です)
  $t0 = [gettimeofday];
  # ここで何か処理の固まりを実行
  $t1 = [gettimeofday];
  # さらに処理の固まりを実行
  $t0_t1 = tv_interval $t0, $t1;

  $elapsed = tv_interval ($t0, [gettimeofday]);
  $elapsed = tv_interval ($t0); # equivalent code
  #
  # replacements for time, alarm and sleep that know about
  # floating seconds
  #
  use Time::HiRes;
  $now_fractions = Time::HiRes::time;
  Time::HiRes::sleep (2.5);
  Time::HiRes::alarm (10.6666666);
  #
  # time, alarm そして sleepを浮動小数点での秒がわかる
  # もので置き換え
  #
  use Time::HiRes;
  $now_fractions = Time::HiRes::time;
  Time::HiRes::sleep (2.5);
  Time::HiRes::alarm (10.6666666);

  use Time::HiRes qw ( time alarm sleep );
  $now_fractions = time;
  sleep (2.5);
  alarm (10.6666666);
  # Arm an interval timer to go off first at 10 seconds and
  # after that every 2.5 seconds, in process virtual time
  # プロセス仮想時間で最初に10秒、その後は2.5秒ごとに
  # やってくるインターバルタイマーを装備する

  use Time::HiRes qw ( setitimer ITIMER_VIRTUAL time );

  $SIG{VTALRM} = sub { print time, "\n" };
  setitimer(ITIMER_VIRTUAL, 10, 2.5);

C API

In addition to the perl API described above, a C API is available for extension writers. The following C functions are available in the modglobal hash:

拡張を書く人には上記のperl APIに加えて、C APIを利用することが 利用できます。以下のCの関数がmodglobalハッシュで利用することが できます:

  name             C prototype
  ---------------  ----------------------
  Time::NVtime     double (*)()
  Time::U2time     void (*)(UV ret[2])
  関数名           Cプロトタイプ
  ---------------  ----------------------
  Time::NVtime     double (*)()
  Time::U2time     void (*)(UV ret[2])

Both functions return equivalent information (like gettimeofday) but with different representations. The names NVtime and U2time were selected mainly because they are operating system independent. (gettimeofday is Unix-centric, though some platforms like VMS have emulations for it.)

関数は両方とも(gettimeofdayのような)同じ情報を返します。 しかしその表現が違います。NVtimeU2timeという名前は主に オペレーティング・システムに依存していないために選択されています。 (VMSのようにいくつかのプラットホームではそのエミュレーションを 備えていますが、gettimeofdayはUnix中心的です。)

Here is an example of using NVtime from C:

CからNVtimeを使ったときの例を以下に示します:

  double (*myNVtime)();
  SV **svp = hv_fetch(PL_modglobal, "Time::NVtime", 12, 0);
  if (!svp)         croak("Time::HiRes is required");
  if (!SvIOK(*svp)) croak("Time::NVtime isn't a function pointer");
  myNVtime = INT2PTR(double(*)(), SvIV(*svp));
  printf("The current time is: %f\n", (*myNVtime)());

DIAGNOSTICS

negative time not invented yet

You tried to use a negative time argument.

引数としてマイナスの時刻を使おうとしました。

internal error: useconds < 0 (unsigned ... signed ...)

Something went horribly wrong-- the number of microseconds that cannot become negative just became negative. Maybe your compiler is broken?

何かひどい障害がおきました -- マイナスになるはずのないマイクロ秒数は マイナスになってしまいました。コンパイラが壊れているのかも?

CAVEATS

Notice that the core time() maybe rounding rather than truncating. What this means is that the core time() may be reporting the time as one second later than gettimeofday() and Time::HiRes::time().

コアのtime()が切り捨てるのではなく、四捨五入するかもしれないことに 注意してください。これはコアのtime()gettimeofday()Time::HiRes::time()よりも1秒遅い時間を報告するかもしれないという ことを意味します。

作者

D. Wegscheid <wegscd@whirlpool.com> R. Schertler <roderick@argon.org> J. Hietaniemi <jhi@iki.fi> G. Aas <gisle@aas.no>

コピーライト & ライセンス

Copyright (c) 1996-2002 Douglas E. Wegscheid. All rights reserved.

Copyright (c) 2002,2003,2004 Jarkko Hietaniemi. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

翻訳者

川合孝典(GCD00051@nifty.ne.jp)