简体   繁体   中英

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:

<?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. And now there are 3 - I just made the update views a one liner.

# 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.

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. So it wouldn't drastically slow down your script execution.

When using a PHP cache, includes really don't matter that much. 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:

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

My parser then reads the PHP file and notices this include. 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). It then gets saved to the bin directory, which is where the live files are.

This approach works very well, but you must always check for the <?php and ?> tags. Also, you'd have to split src and bin directories, because you can't change anything that's already live anymore.

Your primary focus area for optimizations should probably be the database though, and other CPU-intensive operations such as loops.

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. 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). PHP code doesn't go across the network. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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