Hudzilla.org - the homepage of Paul Hudson
Contents > Performance > Optimising your code Wish List | Report Bug | About Me ]

18.1.17     Don't use dl()

This is NOT the latest copy of this book; click here for the latest version.

The dl() function allows you to dynamically load a particular PHP extension for use in a single script then unload it when that script is done. While this is certainly very useful, consider what happens if you have that script being executed once every couple of seconds - PHP would need to continuously load the extension, execute the code, then unload it again, only to repeat the process a few seconds later.

In comparison, extensions that are enabled in the php.ini file are loaded once, when your web server is started, and unloaded only when the web server is stopped. This means that any scripts that take advantage of these modules execute a lot (lot) faster.

To give you an idea of the difference using dl() can make, I wrote a simple test script - in one, I used dl() to dynamically load the MySQL extension, connect to a database, then disconnect again. In the other, the MySQL extension was enabled in the php.ini file, there was resident in memory all the time, and it was also used to connect to a database then disconnect again.

Here is the two scripts:

<?php
    dl
('php_mysql.dll');

    
mysql_connect("localhost", "phpuser", "alm65z");
    
mysql_close();
?>
<?php
    mysql_connect
("localhost", "phpuser", "alm65z");
    
mysql_close();
?>

Simple enough, right? Well, I ran each script through the Apache benchmarking tool, which called each of them 10,000 times individually and returned the amount of time each took to execute - the former took 26.88 seconds to complete, and the latter took 4.98 seconds. From this test, dl() slowed the script down by a factor of five, which should make it crystal clear that you should enable your extensions in your php.ini file, and not by using dl() unless you really cannot help it.





<< 18.1.16 Don\'t use CGI   18.1.18 Debug your code >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is ten plus three?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow