Is there a better way to determine elapsed time in Perl?

前端 未结 4 1413
囚心锁ツ
囚心锁ツ 2021-02-01 12:47
my $start_time = [Time::HiRes::gettimeofday()];
my $diff = Time::HiRes::tv_interval($start_time);

print \"\\n\\n$diff\\n\";
4条回答
  •  无人共我
    2021-02-01 13:47

    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.

    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);
    

    or:

    use Time::HiRes qw( time );
    
    my $begin_time = time();
    ...
    my $end_time = time();
    printf("%.2f\n", $end_time - $begin_time);
    

提交回复
热议问题