Listing 3–14 sets the variable $fileToFetch to a small file we are using for this test
containing 3.9KB of data stored locally. Replace the
to the path of your locally stored file if you plan to run the code yourself. The code sets
our start time using microtime() and uses a for loop to run the code, which uses
file_get_contents() to access the file and read its content 100,000 times. Finally we
calculate the execution time and display it on screen.
Once the PHP code is done executing ten times, we calculate the average execution
using each run’s result. Using file_get_contents had an average execution time of
0.3730ms.
We will compare both methods, fread() as well as file_get_contents(), on two
scenarios, returning data as a string on a small 3.9KB file, and then returning the data
from a large 2.3MB file (Listing 3–14).