有没有更好的方法来确定 Perl 中的经过时间? [英] Is there a better way to determine elapsed time in Perl?

查看:60
本文介绍了有没有更好的方法来确定 Perl 中的经过时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

my $start_time = [Time::HiRes::gettimeofday()];
my $diff = Time::HiRes::tv_interval($start_time);

print "\n\n$diff\n";

推荐答案

可能.取决于你所说的更好"是什么意思.

Possibly. Depends on what you mean by "better".

如果您要在功能方面的更好"解决方案,那么差不多就是这样.

If you are asking for a "better" solution in terms of functionality, then this is pretty much it.

如果您要求更好"的不那么尴尬"的符号,那么请知道,在标量上下文中,Time::HiRes::gettimeofday() 将返回自纪元以来的浮动秒数(小数部分代表微秒),就像 Time::HiRes::time()(这是标准 time() 函数的一个很好的替代品.)

If you are asking for a "better" in the sense of "less awkward" notation, then know that, in scalar context, Time::HiRes::gettimeofday() will return floating seconds since epoch (with the fractional part representing microseconds), just like Time::HiRes::time() (which is a good drop-in replacement for the standard time() function.)

my $start_time = Time::HiRes::gettimeofday();
...
my $stop_time = Time::HiRes::gettimeofday();
printf("%.2f\n", $stop_time - $start_time);

或:

use Time::HiRes qw( time );

my $begin_time = time();
...
my $end_time = time();
printf("%.2f\n", $end_time - $begin_time);

这篇关于有没有更好的方法来确定 Perl 中的经过时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆