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 ...
.
Other issues...
Do not use the deprecated mysql_*
interface; change to mysqli_*
or PDO
.
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.)