user784637
user784637

Reputation: 16152

How to calculate execution time (php directive 'max_execution_time'? )

A few days ago wrote a php script that goes through all my music and reads id3 tags for each song and inserts those in a mysql database. Here's a snippet for context:

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
}

I changed the php directive max_execution_time in apache2/php.ini to gain a better understanding of what this directive does.

When I set max_execution_time = 10, my php script runs for about 45 seconds and successfully reads the id3 tags for about 150 songs (Out of thousands of songs) and inserts those tags into a mysql database before terminating the script and outputting the following to the screen:

Fatal error: Maximum execution time of 10 seconds exceeded in /websites/.../public_html/GetID3()/getid3/module.audio.mp3.php on line 1894

From the documentation, 'The maximum execution time is not affected by system calls, stream operations etc' http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time

  1. What can I infer from the difference between the maximum_execution_time being set at 10 seconds and the script running for a total of 45 seconds before terminating? Does this mean out of the 45 seconds, 35 were spent doing non-php related activities like reading the id3 tags, inserting data into mysql etc..., while 10 were spent doing php related activities like iterating the loop?

  2. Is there a way I can calculate the execution time and print it to the screen?

EDIT Using the timer Dagon suggested, I called the getTime() function at the end of the loop, there were about 100+ iterations of the loop. Here is the output to my browser:

0.1163 seconds

0.8142 seconds

1.1379 seconds

1.5555 seconds

...

76.7847 seconds

77.2008 seconds

77.6071 seconds

Fatal error: Maximum execution time of 10 seconds exceeded in /websites/.../public_html/GetID3()/getid3/module.audio.mp3.php on line 505

Upvotes: 1

Views: 3993

Answers (4)

Vishnu Choudhary
Vishnu Choudhary

Reputation: 421

<!-- 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";
?>

Upvotes: 3

Ranty
Ranty

Reputation: 3362

Seems like you not only try to measure the script duration, but also try to limit it. And in your case max_execution_time is not what you want.

Basically, "The maximum execution time is not affected by system calls, stream operations etc" is correct. If you need to limit real-time script length, you need to implement your own time measurement. People usually write some benchmark class for it, which after all will be helpful in optimizing the script, but simple

$timer['start'] = time();
$timer['max_exec_time'] = 10; // seconds

at start and

if (time() > $timer['start'] + $timer['max_exec_time'])
    break; // or exit; etc

at the end of the loop or anywhere else you want should be enough.

Upvotes: 1

user895378
user895378

Reputation:

This type of script should really be executed in the CLI environment, not in a php process executed by your web server. As per the manual docs on how the PHP command line environment differs from other PHP SAPIs:

PHP in a shell environment tends to be used for a much more diverse range of purposes than typical Web-based scripts, and as these can be very long-running, the maximum execution time is set to unlimited.

While it doesn't answer your question, it does solve your problem :)

Upvotes: 1

user557846
user557846

Reputation:

i don't believe the script is actully running for more than 10 seconds, you need to put a proper timer in it

<!-- 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;
?>

Upvotes: 1

Related Questions