Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用perl精确计时系统调用的执行_Perl - Fatal编程技术网

使用perl精确计时系统调用的执行

使用perl精确计时系统调用的执行,perl,Perl,有没有更好的方法以编程方式使用perl跟踪unix进程的计时(开始时间、结束时间) 例如,我有一个perl脚本,它使用system()启动unix命令。我在system()之前和之后打印时间戳 我的开始时间和结束时间日志是相同的。我可以肯定的是,当这个过程完成时,它只需要几分钟,所以它并不准确 以下是我迄今为止的代码: my $dt = strftime('%Y%m%d', localtime(time)); my $dt2 = strftime('%Y%m%d-%H:%M:%S', loca

有没有更好的方法以编程方式使用perl跟踪unix进程的计时(开始时间、结束时间)

例如,我有一个perl脚本,它使用system()启动unix命令。我在system()之前和之后打印时间戳

我的开始时间和结束时间日志是相同的。我可以肯定的是,当这个过程完成时,它只需要几分钟,所以它并不准确

以下是我迄今为止的代码:

my $dt  = strftime('%Y%m%d', localtime(time));
my $dt2 = strftime('%Y%m%d-%H:%M:%S', localtime(time)); 
my @tbls = qx(mysql -u foo -pmypw --database $dbsrc -h $node -ss -e "show tables");

open (CRLOG, ">dump-$dt.log") || die "cannot append";
foreach my $tbls (@tbls)
{
   chomp $tbls;
   print CRLOG "TIME START => $dt2\n";
   my $crfile = qq{mysql -u foo -pmypw --database $dbsrc -h $node.test.com -ss -e "SELECT 'a','b','c' UNION SELECT 1,2,3 FROM $tbls"| tr "\t" ",">$node-$tbls.$dt.csv};   system($crfile); 
   print CRLOG "COMMAND => $crfile\n";
   print CRLOG "TIME END => $dt2\n";
}; 
close (CRLOG);

$dt2
是一个常量,但您似乎希望它在每次使用时重新计算时间戳。为此,您应该使用函数

sub dt2 {
    return strftime('%Y%m%d-%H:%M:%S', localtime(time))
}

...

open (CRLOG, ">dump-$dt.log") || die "cannot append";
foreach my $tbls (@tbls)
{
   chomp $tbls;
   print CRLOG "TIME START => ", &dt2, "\n";
   my $crfile = qq{mysql -u foo -pmypw ...};
   system($crfile); 
   print CRLOG "COMMAND => $crfile\n";
   print CRLOG "TIME END => ", &dt2, "\n";
}; 
close (CRLOG);
您是否尝试过使用“time”命令为命令执行计时

因此,在您的perl脚本中,捕获输出的方式如下:

$time_me = `/usr/bin/time $my_command`;

然后解析$time\u me变量以获取值。

谢谢!这很酷。它工作得很好!在我的小表示例测试中,我可以看到秒数的变化。如果您需要更高的粒度,也可以尝试
use Time::HiRes qw/Time/
不,我没有尝试过,但谢谢!我也会将此作为一个选项进行检查。您在调用命令之前而不是之后获取
$dt2