Question
Since I am using multiple instances of home_url()
in one page template, I was curious if the following method would speed up my page load time. My method is to store the output of home_url()
within a variable and then call that variable each time, rather than using home_url()
each time.
Motivation
The reason I thought echoing a variable would be speed up the page load time, is because (if my understanding is correct) echoing a variable wouldn’t require querying the database, each time I called that variable.
Method A
<ul>
<li><a href="<?php echo home_url(); ?>/category/blue">Blue Items</a></li>
<li><a href="<?php echo home_url(); ?>/category/green">Green Items</a></li>
<li><a href="<?php echo home_url(); ?>/category/red">Red Items</a></li>
<li><a href="<?php echo home_url(); ?>/category/orange">Orange Items</a></li>
<li><a href="<?php echo home_url(); ?>/category/purple">Purple Items</a></li>
<li><a href="<?php echo home_url(); ?>/category/gray">Gray Items</a></li>
</ul>
Method B
<?php $my_home_url = home_url(); ?>
<ul>
<li><a href="<?php echo $my_home_url; ?>/category/blue">Blue Items</a></li>
<li><a href="<?php echo $my_home_url; ?>/category/green">Green Items</a></li>
<li><a href="<?php echo $my_home_url; ?>/category/red">Red Items</a></li>
<li><a href="<?php echo $my_home_url; ?>/category/orange">Orange Items</a></li>
<li><a href="<?php echo $my_home_url; ?>/category/purple">Purple Items</a></li>
<li><a href="<?php echo $my_home_url; ?>/category/gray">Gray Items</a></li>
</ul>
If you look at the source for the
home_url()
function, you’ll note a small series of function calls eventually invokingget_option()
. As explained in this WPSE Answer, theget_option()
function is cached, meaning that if an option’s value is already in memory,get_option()
returns that value instead of querying the database again. As a result, even if you callhome_url()
a thousand times during one request it will only query the database once.That in mind, Method B (re-using the value from a local variable) still has a slight performance advantage over Method A (calling
home_url()
as needed) in that it doesn’t cycle a bunch of function calls onto the call stack. That said, given modern PHP’s performance, the difference is negligible and could be measured in a number of milliseconds, if not microseconds were one to profile the different approaches.In short, Method B will always out-perform alternate solutions, if only by an infinitesimal measure.
Addendum
As requested in the comments, I have created execution time profiles of both methods for my server environment. In doing so, I created the plugin attached to the bottom of my answer, which you can use to test the methods in your own environment.
In my environment (Varying Vagrant Vagrants‘
wordpress-default
site – Ubuntu Server 12.04 using a PHP 5.4/MySQL 5.5/Nginx 1.4 stack), averaging 100 profiles with 1000echo()
‘d instances, I received the following results:Method A
GET
string:?hup_method=a&hup_profiles=100&hup_instances=1000
Method B
GET
string:?hup_method=b&hup_profiles=100&hup_instances=1000
In this profile, Method B was about 34 times faster than Method A, Method B having
echo()
‘d 1000 instances in 0.7 milliseconds, 23.2 milliseconds faster than Method A managed. This means that it took approximately 23 microseconds (0.000023 seconds) to execute a call tohome_url()
, and less than 0.7 microseconds (0.0000007 seconds) to follow a$homeUrl
reference (much of this time can be attributed to the initial call tohome_url()
used in Method B).If you’re super interested in the complexities of code performance, I recommend spending some time learning about “Big O Notation,” or finding a discrete mathematics computer science course.
Setting the
GET
variablehup_method
will run a profile for method A or B depending on the value specified. TheGET
variablehup_instances
can be used to specify the number of home URLs to echo for the method in a profile (defaults to 1000), and the variablehup_profiles
can be used to specify the number of times you would like to run the profile to create an average (defaults to 5).There are various ways in which this profile could be made more accurate (subtracting the amount of time it takes to perform the
for
loop, for instance), but in it’s present form it gives a pretty good general idea of the timeframes involved.Cheers!
You’re absolutely right in your assumption. Look at the actual definition of
get_home_url()
inwp-includes/link-template.php
.aside:
home_url()
is just a convenience method that echosget_home_url()
.Below is the function definition. By assigning it’s output to a variable, you’re avoiding the performance impact of calling this code 5 more times than necessary: