i want to test actual speed of script.
I have run below query.
$query="select * from test";
$msc = microtime(true);
for ($i = 0; $i <= 99999; $i++)
$rs = mysql_query($query);
$msc = microtime(true) - $msc;
echo $msc . ' s'; // in seconds
echo ($msc * 1000) . ' ms'; // in milliseconds
I try above code with different optimization queries. still have same output
$query="select * from stud";
$query2="select name from stud"
have same result run both query run thousand time using for loop.. please is there any other proper way to find out actual execution time of script.
What if you write your code in
second.phpfile and Let
first.phpfile to calculate the actual execution time of script.
# file: `first.php` $init_time = time(); # Execute Your Script `second.php` # Assuming php is properly installed $output = `php second.php`; $exit_time = time(); echo $exit_time - $init_time;
Edit: May be, You are Right, You will get same execution time because in both cases, You are just Running those
MySQLQueries 1000 times. You are not processing data returned by PHP-MySQL Driver.
You get good enough timings with a single iteration. However,...
The first execution of a query may be a lot slower than the second (and all subsequent calls -- very consistent timings). The first execution may hit cold caches -- hence more I/O to be done. So...
I would time the second execution of the same statement. But...
If you have the "Query cache" turned on, the second one will be "instantaneous" so not a good timing. So...
Always perform timing tests thus:
SELECT SQL_NO_CACHE ....
Do not use the deprecated
mysql_* interface; change to
In your particular code, you probably should have divided by 100000 because of the repetitions.
For something like a MySQL statement, the overhead of the
FOR loop is only a small percentage. But if you time some faster function (most PHP builtin functions), you need to estimate the FOR loop time and subtract it off. (That can be done by timing an empty loop. PHP is not smart enough to optimize out useless code. Nor is MySQL at the statement level. MySQL will clean up things like
WHERE 1=1 AND ....)
(I know all this because I usually pepper my PHP code with
microtime(true) calls. Usually I do it at the subroutine level; later drill down to individual SQL queries if necessary.)
You initially said that you want to measure PHP code execution speed, but I am afraid that what you're actually after is benchmarking different SQL queries, so let's cover both cases here.
Plain and simple, use timeit to measure PHP speed. It's not a straightforward task to write code to time code, and there's no need to roll your own.
In this case, you should try to go as direct and close to the MySQL server as possible. This mean running queries in the database server, and not using PHP/webservers, because these can add their own overhead. A good and convenient way to do this is by using the
mysql CLI client:
$ mysql -vvv -q -e 'SELECT SQL_NO_CACHE nid FROM node' drupal | grep 'rows in set' 87281 rows in set (0.00 sec) $ mysql -vvv -q -e 'SELECT SQL_NO_CACHE * FROM node' drupal | grep 'rows in set' 87281 rows in set (0.02 sec)
SQL_NO_CACHEdirective and the