I think no one will give you exact answer why your code is slow. The thing is we don’t know what is inside each file. You have to profile your code. Simplest way to profile code is to use microtime
.
$time = -microtime(true);
// Here exeute code which you want to profile
$time += microtime(true);
echo sprintf('Your code executed in in %fs', $time) . '<br>';
exit;
I would first profile each require
and see which takes the most time to load. This can look something like that.
$format="File %s required in %fs";
$require_plugin_file = function( $file ) use ($format) {
$time = -microtime(true);
require plugin_dir_path( __FILE__ ) . $file;
$time += microtime(true);
echo sprintf($format, $file, $time) . '<br>';
};
$require_plugin_file('file1.php');
$require_plugin_file('file2.php');
$require_plugin_file('file3.php');
$require_plugin_file('file4.php');
$require_plugin_file('file5.php');
exit;
Output of this could be:
File file1.php required in 0.000260s
File file2.php required in 0.000255s
File file3.php required in 3.000257s
File file4.php required in 0.000241s
File file5.php required in 0.000235s
Now I know that my file3.php
take the most time to load and I would see in this file where the problem is.
Note: In sample code I’m using Anonymous functions
which are available from php 5.4
.