0

My question based on Accurate way to measure execution times of php scripts

and question is what if i have a very long code which have a lot of die() or return or exit; functions

and i wan't to calculate script execution speed ( which can be different, depends on params .. )

any way to do it?

my suggestion is:

<?php
$time_start = microtime(true); 
include("script_real_name.php");
$time_end=microtime(true);
$dif=$time_end-$time_start;

/* but question, is this script will work on case of DIE() or EXIT; function called on "script_real_name.php"?

how to make it workable.

and other question, will the included script work with $_POST, $_GET?

*/

Community
  • 1
  • 1
  • see [register_shutdown_function](http://php.net/manual/en/function.register-shutdown-function.php) – Anigel Jun 11 '13 at 10:33

2 Answers2

4

You can do the calculation in a function and register that function for execution on shutdown with register_shutdown_function.

$time_start = microtime(true);

register_shutdown_function(function() use ($time_start) {
    $time_end= microtime(true);
    $dif = $time_end-$time_start;
    echo "Script ran for $dif seconds\n";
});

// do a lot of work and die() suddenly and somewhere
netiul
  • 2,737
  • 2
  • 24
  • 29
1

if you have linux shell access, you could try

$ time php myscript.php

Other than that, you can define the script start time as a CONST at the beginning of the script, and, using your logic, echo the durartion after execution ends. "What" - you might say - "run commands after php die()d?" Yes. Register a function to run after shutdown.

Zsolt Szilagyi
  • 4,741
  • 4
  • 28
  • 44