简体   繁体   English

PHP Include可以使您的代码更快吗?

[英]Does PHP Include make your code faster?

I have an ad network script like the following 我有一个如下的广告网络脚本

http://cdn.domain.com/ad.php?variables=etc

We have about 10,000 hits a second and we are looking at some improvements for our Pseudo code - this is what I have in mind - my question is - would PHP includes slow down my script like the if else codes and would it be worth minifying the PHP on this page: 我们每秒大约有10,000次点击,我们正在寻找伪代码的一些改进-这就是我的想法-我的问题是-PHP是否会像if else代码那样放慢我的脚本的使用,是否值得将其最小化?此页面上的PHP:

<?php
// mysql connect
// get variables from publisher
// if publisher has no ads show advertise here banner 
// if resolution from variables is 125x125 show that banner or whatever resolution from the vars
// example www.noadhere.com/image/advertishere_{var}125px.jpg
// if publisher has no ads show advertise here banner and also updated mysql with page views for this publisher

// if publisher has a banner then show it and update mysql with page views
// show also the click code that redirects and updates the record with a hit click
?>

I have updated the code. 我已经更新了代码。 This is the Phase 1 draft for those who are interested. 这是针对感兴趣者的第一阶段草案。 I think it is so much simpler and I am going to minify this - even though there may not be need - we had 4 mysql actions happening. 我认为这要简单得多,即使没有必要,我也会尽量减少这一点-我们发生了4次mysql操作。 And now there are 3 - I just made the update views a one liner. 现在有3个-我刚刚将更新视图设置为一个班轮。

# mysql
$c=mysql_connect("sqlmaster.adserver.com","user","************");
mysql_select_db("adserver", $c);

# vars
$a=mysql_real_escape_string($_GET["z"]);//id
$z=mysql_real_escape_string($_GET["z"]);//zone
$h=mysql_real_escape_string($_GET["h"]);//height
$w=mysql_real_escape_string($_GET["w"]);//width
$d=date("Y-m-d H:i:s");//date
$u=mysql_real_escape_string($_SERVER['HTTP_REFERER']);//url

# constructor

    # do we have ads?
    $r1=mysql_query("");
    if(mysql_affected_rows()==0){

        # empty ad code unit
        echo 'Blog Empty';

    } else {

        # we have ads - so show random click code
        echo 'Click here .php ? and redirect';

    }

    # update mysql view table for this ad unit - empty or filled
    $r2=mysql_query("");

# end constructor
mysql_close($c);

Any suggestions on improving this would be welcomed. 任何对此进行改进的建议都将受到欢迎。 I think the mysql_real_escape is slow. 我认为mysql_real_escape很慢。

Using include only slows down your script by the amount of time it takes your server to open the file, which is usually just a fraction of a second. 使用include仅会使服务器打开文件所花费的时间减慢脚本的速度,通常只需几分之一秒。 So it wouldn't drastically slow down your script execution. 因此,它不会大大降低脚本的执行速度。

When using a PHP cache, includes really don't matter that much. 使用PHP缓存时,包含内容并没有太大关系。 However, there definitely is a very minor difference. 但是,肯定有很小的差异。

My own build scripts automatically replace includes with "normal" code, using a self-made syntax that's backwards compatible with PHP: 我自己的构建脚本使用与PHP向后兼容的自制语法自动将包含的内容替换为“普通”代码:

/*safeinclude*/ include 'file.php';

My parser then reads the PHP file and notices this include. 然后,我的解析器读取PHP文件,并注意到其中包括。 It grabs the contents of the file.php file and replaces the include with this code (after some cleanup, such as removing the leading <?php tag). 它获取file.php文件的内容,并用此代码替换include(在进行一些清理后,例如删除前导<?php标签)。 It then gets saved to the bin directory, which is where the live files are. 然后将其保存到活动目录所在的bin目录中。

This approach works very well, but you must always check for the <?php and ?> tags. 这种方法效果很好,但是您必须始终检查<?php?>标记。 Also, you'd have to split src and bin directories, because you can't change anything that's already live anymore. 另外,您必须拆分srcbin目录,因为您无法更改已存在的任何内容。

Your primary focus area for optimizations should probably be the database though, and other CPU-intensive operations such as loops. 不过,您的主要优化重点可能应该是数据库以及其他CPU密集型操作(例如循环)。

There are lots of ways of making code run faster. 有很多方法可以使代码运行更快。 Usually splitting code into separate files will not improve performance (selectively including just the code you need instead of a huge library will probably help). 通常,将代码拆分为单独的文件不会提高性能(选择性地仅包含您需要的代码而不是庞大的库可能会有所帮助)。

You may have noticed that there aren't a lot of off-the shelf solutions for minifying PHP code - there's a good reason for that. 您可能已经注意到,没有很多现成的解决方案可用于缩减PHP代码-这是有充分理由的。 It won't make a lot of difference to the execution time (it does for javascript mainly due to the reduction in network transfer time - not in the reduction in parsing time). 它不会对执行时间造成很大的影响(对于javascript而言,主要是由于减少了网络传输时间,而不是因为减少了解析时间)。 PHP code doesn't go across the network. PHP代码不会跨网络传播。 And if you want to reduce parsing time, then using an opcode cache is a much effective solution. 而且,如果您想减少解析时间,那么使用操作码缓存是一种非常有效的解决方案。

Since this is presumably a significant revenue stream, then you should have the skills to know this already. 由于这大概是一笔可观的收入,因此您应该已经具备了了解这一点的技能。 However a lot of performance improvements come about by trial and error (and careful experiment design and measurement) - you might want to invest some time in developing these facilities. 但是,通过反复试验(以及仔细的实验​​设计和测量)可以实现许多性能改进-您可能需要花费一些时间来开发这些设施。

The good thing about running if and else's is that only the code that is needed to be ran will run. 关于if and else的好处是,只有需要运行的代码才能运行。 Included pages are loaded in a split of a second and do not really make any difference on the speed. 所包含的页面仅需瞬间即可加载,并且速度上并没有任何区别。 Most big websites have long trails of included files and you don't really notice it. 大多数大型网站都有很长的内含文件,您并没有真正注意到它。

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

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