Reputation: 10463
I'm creating a theme for my client and they are really picky on the page load time.
So I've thought that the less code will help the page load faster, and I come across the php code to include once.
<?php
include_once "a.php"; // this will include a.php
?>
and if I do with the if statement to include once I have to declare a variable and change the variable to false after the second check
What will be the most efficient coding and help page performance?
I also want to know if there is even better way of coding to help to make the page load faster when we want to execute the code from a file.
Thanks
Upvotes: 0
Views: 1700
Reputation: 56557
you can also do even a very basic benchmark, like running it 1M times and then calculate the average:
<?php
echo microtime(true)."\n";
for ($i=0; $i<1000000; $i++) {
include_once (__DIR__ .'/file.php');
}
echo microtime(true)."\n";
Upvotes: 0
Reputation: 31780
It might have been true in the dim and distant past that include_once was a lot slower than include, but that was the dim and distant past. PHP's include_once functionality has been optimized heavily since then. Unfortunately, there's still lots of old articles floating around on the internet that make the claim that include_once is slow, even though it's no longer true.
Even if include_once was a lot slower than include, the odds are it wouldn't cause an appreciable performance impact unless you were including thousands and thousands of files. Investing time on speeding it up is a micro-optimization, especially if you have no evidence that it's a bottleneck in your code.
First and foremost in any project is getting the code to work to specifications. Code that's slow but works is still better than code that's fast but doesn't work. Once you've got the code working and passing all its unit tests (you are using unit tests, right?) then you can start worrying about performance. And when you get to that point the first thing you should do is profile your code to discover where the actual bottlenecks are, not start guessing at where you think they might be.
Upvotes: 3
Reputation: 41944
In my opinion PHP doesn't affect the file executing time very much. Unless you are looping trough 1000 loops or results I think you can't really speed up the file executing time. So I suggest you to don't worry about these things.
I should use the include_once
/require_once
because it is a PHP build-in function, simple to use and exactly what you need.
Upvotes: 2
Reputation: 1970
Based on what is suggested here and there. require_once is the fastest. There is also __autoload
, but with some performance draw backs. As it is suggested here it matters if you use relative or absolute addressing too.
Upvotes: 1