[D][Perl] 如何可以往命令行动态输出当前脚本已运行的时间

whn1119 2012-05-14 03:01:05
新手提问:

写了一个简单的perl脚本,不过运行时间比较长,我想在等待的过程中能在命令行看到当前脚本运行了多长时间了。比如在命令行输出

the script is running for: XX

其中的XX想让他动态变化,能实现吗?

谢谢!
--------------------
Double行动:
原帖分数:40
帖子加分:40
...全文
302 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
busybeeCS 2012-06-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

Perl code
use strict;
use warnings;
use threads ('yield','stack_size' => 64*4096,'exit' => 'threads_only','stringify');
use threads::shared;

use Time::HiRes qw( usleep );

my $done:shared=0;
my $el……
[/Quote]
能解释一下
use threads ('yield','stack_size' => 64*4096,'exit' => 'threads_only','stringify');
use threads::shared;

use Time::HiRes qw( usleep );

lock($done);
都是什么意思吗
panghuhu250 2012-05-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

楼主是想要其中的XX想让他动态变化,
也就是第一秒出现:
The script is running for: 1 seconds
第二秒变化成:
The script is running for: 2 seconds
而不是重新输出一句,变成下面这样子:
The script is running for: 1 seconds
The script is running for……
[/Quote]

(给象我一样不明白的人)
3楼的建议就是实现这个效果。
薛定谔之死猫 2012-05-16
  • 打赏
  • 举报
回复
那么麻烦干什么,直接$time perl -W scriptfile.pl
chen_999876 2012-05-16
  • 打赏
  • 举报
回复
楼主是想要其中的XX想让他动态变化,
也就是第一秒出现:
The script is running for: 1 seconds
第二秒变化成:
The script is running for: 2 seconds
而不是重新输出一句,变成下面这样子:
The script is running for: 1 seconds
The script is running for: 2 seconds

在输出面板里好像没有这种动态变化的吧,如果有,那也是先把前面的清空了,然后再显示。
想问一下楼主,你的脚本运行过程中本身有输出的吗?如果有的话,像2楼的代码会把你自身的输出给淹没掉,如果没有的话,这个也可以啦!
fibbery 2012-05-15
  • 打赏
  • 举报
回复
use strict;
use warnings;
use threads ('yield','stack_size' => 64*4096,'exit' => 'threads_only','stringify');
use threads::shared;

use Time::HiRes qw( usleep );

my $done:shared=0;
my $elapse=0;
sub displayElapseTime
{
#禁止标准输出缓冲区
select(STDOUT);
$|=1;

#循环显示时间
while(1)
{
sleep(1);
print("running in ",++$elapse," seconds\r");
lock($done);
if($done)
{
print("\n");
last;
}
}
}

my $t=threads->create(\&displayElapseTime);

my $count=0;
foreach (1..100000000)
{
++$count;
}
#设置结束状态
{#限定lock作用域
lock($done);
$done=1;
}
$t->join();

print("The count is $count.\n");
jiuchang 2012-05-15
  • 打赏
  • 举报
回复
在输出的最后用\r,而不是\n,这样的屏幕上看起来就有效果了
panghuhu250 2012-05-14
  • 打赏
  • 举报
回复
/tmp/ cat /tmp/x.pl
use utf8;
$start=time;
$|=1; # 及时把输出显示在屏幕上
while (1) {
sleep(1); # do some work

# now it's time to report
print("The script is running for: ", time-$start, " seconds\n");
}

/tmp/ perl /tmp/x.pl
The script is running for: 1 seconds
The script is running for: 2 seconds
The script is running for: 3 seconds
The script is running for: 4 seconds
The script is running for: 5 seconds
The script is running for: 6 seconds
^
fibbery 2012-05-14
  • 打赏
  • 举报
回复
使用多线程实现。

37,741

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧