函数执行时间在PHP中第二次减少很多

I'm using php 7.1.0. The script I'm benchmarking is this one:

$myclass = new MyClass();

$t1 = microtime(true);
myfunction();
echo microtime(true)-$t1;

$t2 = microtime(true);
myfunction();
echo microtime(true)-$t2;

Results are:

~30 milliseconds the first time
~0.7 milliseconds the second time.

And this is valid not only for the second time but also for the following ones.

I'm asking why there is such difference between the first execution and the following ones. I think it's a problem of loading the functions in memory, once loaded it is as it should be but I also suppose that ~30ms are too much to load the function in memory.

Other informations:

  • myfunction has an $options array as input and return an allocated object.
  • It happens also with static function with an improvement of 6ms for the first time. The following times are almost the same.
  • The script load classes using composer autoloading.
  • The php engine and the apache2 server are installed on a embedeed card with processor: 4x Cortex-A9@800MHz and RAM: 2GB DDR3-1066MHz.

Thank you