简体   繁体   English

Wordpress the_permalink performance vs将值存储在变量中

[英]Wordpress the_permalink performance vs Storing the value in a variable

What could be the most efficient way to make a new Theme and create an article view with multiple objects that link to said article? 什么是最有效的方式来制作一个新的主题和创建一个文章视图与链接到所述文章的多个对象? I am a C# pro but in PHP I am not as well versed as I wish I was. 我是C#专业版,但在PHP中我并不像我希望的那样精通。 Suppose you have: 假设你有:

while(have_posts())
    <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
    <a href="<?php the_permalink(); ?>"><?php the_thumbnail(); ?></a>
    <a href="<?php the_permalink(); ?>">read more</a>

As you can see we have at least 3 calls to the function the_permalink(); 如您所见,我们至少有3次调用函数the_permalink();

Is it faster to call the function three times, or rather call it one time, save it in a variable, and then throw the variable within the loop as much as needed? 调用函数三次更快,或者更确切地说是一次调用它,将它保存在变量中,然后根据需要将变量放入循环中?

While there would be less CPU load to do this, it's a case of premature optimization. 虽然执行此操作的CPU负载较少,但这是一个过早优化的情况。 The benefit you get isn't that great, particularly because this call doesn't have to touch the database. 您获得的好处并不是那么好,特别是因为此调用不必触及数据库。 Once you factor in the fact that what takes the longest with PHP is compiling the code, I'd be surprised if you saw any benefit in a benchmark. 一旦你考虑到使用PHP花费最长时间的事实就是编译代码,如果你在基准测试中看到任何好处,我会感到惊讶。

If you dig into the get_permalink() function (in wp-includes/link-template.php) you'll note that the method only consults the options store, which is loaded once on WP initialization. 如果你深入了解get_permalink()函数(在wp-includes / link-template.php中),你会注意到该方法只参考了在WP初始化时加载一次的选项存储。

If you're trying to speed up the site, 99% of the time the way to do it is to cut down on database calls. 如果您正在尝试加速网站,99%的方法是减少数据库调用。 I'd focus your efforts there :) 我会把你的努力集中在那里:)

I was curious, so I benched with the following code: 我很好奇,所以我使用以下代码进行了操作:

ob_start();
$bench = microtime(true);
for ($i = 0; $i < 1000; ++$i) {
    the_permalink();
    the_permalink();
    the_permalink();
}
$bench2 = microtime(true);
ob_end_clean();
echo ($bench2 - $bench) . '<br>';
ob_start();
$bench = microtime(true);
for ($i = 0; $i < 1000; ++$i) {
    $permalink = get_permalink();
    echo $permalink;
    echo $permalink;
    echo $permalink;
}
$bench2 = microtime(true);
ob_end_clean();
echo ($bench2 - $bench) . '<br>';

And got the following result: 得到以下结果:

the_permalink(): 1.891793012619
Storing in a variable and echoing: 0.62593913078308

So storing in a variable and echoing is substantially faster if there are a large number of calls, but for just three calls the performance improvement is only going to be a bit more than one one-thousandth of a second. 因此,如果存在大量调用,则存储在变量和回显中的速度要快得多,但对于仅三次调用,性能改进仅会超过千分之一秒。

Note that some filters are called every time you call the_permalink() too (eg the_permalink, post_link, etc.) so the speed gain from storing in a variable may be higher depending on how many hooks there are into those filters and what they do. 请注意,每次调用the_permalink()时都会调用一些过滤器(例如the_permalink,post_link等),因此存储在变量中的速度增益可能会更高,具体取决于这些过滤器中有多少个钩子以及它们的作用。

It would definitely be much less backend processing to store it in a variable than to call it 3 times in a row per your example. 将它存储在一个变量中的后端处理肯定比根据你的例子连续调用它多3次。 Since the_permalink() echos the permalink, you'll have to use get_permalink() to store it in the variable. 由于the_permalink()回显永久链接,因此您必须使用get_permalink()将其存储在变量中。

<?php
while(have_posts()) {
    $permalink = get_permalink();
?>
<h4><a href="<?php echo $permalink; ?>"><?php the_title(); ?></a></h4>
<a href="<?php echo $permalink; ?>"><?php the_thumbnail(); ?></a>
<a href="<?php echo $permalink; ?>">read more</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM