如何计算执行时间(php指令';最大执行时间';?)
几天前,我编写了一个php脚本,它遍历我所有的音乐,读取每首歌曲的id3标记,并将这些标记插入mysql数据库。以下是上下文的一个片段:如何计算执行时间(php指令';最大执行时间';?),php,bash,shell,Php,Bash,Shell,几天前,我编写了一个php脚本,它遍历我所有的音乐,读取每首歌曲的id3标记,并将这些标记插入mysql数据库。以下是上下文的一个片段: exec ( "find /songs -type f -iname '*mp3'", $song_path ); $number_of_songs = count($song_path); for($i=0; $i<$number_of_songs; $i++){ //read id3 tags //store id3 tags int
exec ( "find /songs -type f -iname '*mp3'", $song_path );
$number_of_songs = count($song_path);
for($i=0; $i<$number_of_songs; $i++){
//read id3 tags
//store id3 tags into database
}
exec(“find/songs-type f-iname'*mp3'$song_path);
$number_of_songs=计数($song_path);
对于($i=0;$i我认为脚本实际运行时间不超过10秒,您需要在其中设置一个适当的计时器
<!-- put this at the top of the page -->
<?php
function getTime() {
$timer = explode( ' ', microtime() );
$timer = $timer[1] + $timer[0];
return $timer;
}
$start = getTime();
?>
<!-- put other code and html in here -->
<!-- put this code at the bottom of the page -->
<?php
$end = getTime();
$time_took= round($end - $start,4).' seconds';
echo $time_took;
?>
我认为脚本实际运行时间不会超过10秒,您需要在其中设置一个适当的计时器
<!-- put this at the top of the page -->
<?php
function getTime() {
$timer = explode( ' ', microtime() );
$timer = $timer[1] + $timer[0];
return $timer;
}
$start = getTime();
?>
<!-- put other code and html in here -->
<!-- put this code at the bottom of the page -->
<?php
$end = getTime();
$time_took= round($end - $start,4).' seconds';
echo $time_took;
?>
这类脚本实际上应该在CLI环境中执行,而不是在web服务器执行的php进程中执行。关于php命令行环境与其他php SAPI的区别,请参见:
shell环境中的PHP倾向于用于更加多样化的应用程序
比典型的基于Web的脚本更广泛的用途
非常长时间运行,最大执行时间设置为无限制
虽然它没有回答您的问题,但它确实解决了您的问题:)这种脚本应该在CLI环境中执行,而不是在web服务器执行的php进程中执行。关于PHP命令行环境与其他PHP SAPI的区别,请参见:
shell环境中的PHP倾向于用于更加多样化的应用程序
比典型的基于Web的脚本更广泛的用途
非常长时间运行,最大执行时间设置为无限制
虽然它不能回答您的问题,但它确实解决了您的问题:)看起来您不仅要测量脚本的持续时间,还要限制它。在您的情况下,max\u execution\u time
不是您想要的
基本上,“最大执行时间不受系统调用、流操作等的影响”是正确的。如果需要限制实时脚本长度,则需要实现自己的时间度量。人们通常会为它编写一些基准类,这毕竟有助于优化脚本,但很简单
$timer['start'] = time();
$timer['max_exec_time'] = 10; // seconds
开始和结束时
if (time() > $timer['start'] + $timer['max_exec_time'])
break; // or exit; etc
在循环结束时或任何您想要的地方都应该足够了。似乎您不仅要尝试测量脚本持续时间,还要尝试限制它。在您的情况下,max\u execution\u time
不是您想要的
<!-- Paste this at the top of the page -->
<?php
$mic_time = microtime();
$mic_time = explode(" ",$mic_time);
$mic_time = $mic_time[1] + $mic_time[0];
$start_time = $mic_time;
?>
<!-- Write Your script(executable code) here -->
enter code here
<!-- Paste this code at the bottom of the page -->
<?php
$mic_time = microtime();
$mic_time = explode(" ",$mic_time);
$mic_time = $mic_time[1] + $mic_time[0];
$endtime = $mic_time;
$total_execution_time = ($endtime - $start_time);
echo "Total Executaion Time ".$total_execution_time." seconds";
?>
基本上,“最大执行时间不受系统调用、流操作等的影响”是正确的。如果需要限制实时脚本长度,则需要实现自己的时间度量。人们通常会为它编写一些基准类,这毕竟有助于优化脚本,但很简单
$timer['start'] = time();
$timer['max_exec_time'] = 10; // seconds
开始和结束时
if (time() > $timer['start'] + $timer['max_exec_time'])
break; // or exit; etc
在循环的末尾或任何你想要的地方就足够了。
<!-- Paste this at the top of the page -->
<?php
$mic_time = microtime();
$mic_time = explode(" ",$mic_time);
$mic_time = $mic_time[1] + $mic_time[0];
$start_time = $mic_time;
?>
<!-- Write Your script(executable code) here -->
enter code here
<!-- Paste this code at the bottom of the page -->
<?php
$mic_time = microtime();
$mic_time = explode(" ",$mic_time);
$mic_time = $mic_time[1] + $mic_time[0];
$endtime = $mic_time;
$total_execution_time = ($endtime - $start_time);
echo "Total Executaion Time ".$total_execution_time." seconds";
?>
在这里输入代码
在这里输入代码
您是否正在使用设置时间限制()调用set_time_limit()时,将从零重新启动超时计数器。换句话说,如果超时是默认的30秒,并且在脚本执行25秒后进行了诸如set_time_limit(20)之类的调用,则脚本将在超时之前总共运行45秒。“不,我没有使用set_time_limit,您是否恰好使用set_time_limit()?”调用set_time_limit()时,将从零重新启动超时计数器。换句话说,如果超时是默认的30秒,并且在脚本执行25秒后进行了诸如set_time_limit(20)之类的调用,那么脚本将在超时之前总共运行45秒。“不,我没有使用set_time_limitHey rd,谢谢。我通过cli处理脚本,但是在这种情况下,我想创建一个用户可以登录/注销的网站,这样我就可以解析他们的id3标签,这就是为什么我要从cgi测试它。顺便说一句,你很像亚当·莱文:栗色歌手5@LedZeppelin哈哈,当我住在洛杉矶的时候,我常常在深夜和喝醉的女孩说同样的话。至于你的网站用例。。。最好生成一个新进程来处理标记解析。这样,您仍然可以利用“CLI”环境。此外,对于这种类型的处理作业,您应该考虑使用某种队列系统,因为这样的长处理作业不适合单个Web服务器PHP进程。谢谢您的建议,如果我完成这个项目,我将转到AmazonEC2,并获得一些高cpu保留实例,但现在我只是在房间里接触服务器。嘿,rd,谢谢。我通过cli处理脚本,但是在这种情况下,我想创建一个用户可以登录/注销的网站,这样我就可以解析他们的id3标签,这就是为什么我要从cgi测试它。顺便说一句,你很像亚当·莱文:栗色歌手5@LedZeppelin哈哈,当我住在洛杉矶的时候,我常常在深夜和喝醉的女孩说同样的话。至于你的网站用例。。。最好生成一个新进程来处理标记解析。这样,您仍然可以利用“CLI”环境。此外,对于这种类型的处理作业,您应该考虑使用某种队列系统,因为这样的长处理作业不适合单个Web服务器PHP进程。谢谢您的建议,如果我完成这个项目,我将转到AmazonEC2并获得一些高cpu保留实例,但现在我只是在房间里接触服务器。