14

I have read that when including a php file that using absolute paths has a faster processing time than relative paths.

What would you suggest to use?

include("includes/myscript.php");

or

include("/home/ftpuser/public_html/includes/myscript.php");

or even

set_include_path("/home/ftpuser/public_html/includes");
include("myscript.php");

Or is it something that I really shouldn't worry about?

Sankar V
  • 4,794
  • 3
  • 38
  • 56
Lizard
  • 43,732
  • 39
  • 106
  • 167

8 Answers8

16

I usually set a constant, either manually or like this:

define('ROOT', dirname(__FILE__));

Then do

require ROOT . '/include/file.php';
Greg
  • 316,276
  • 54
  • 369
  • 333
  • If you have `ROOT`, why not `set_include_path(get_include_path().PATH_SEPARATOR.ROOT)`? – chelmertz Nov 18 '09 at 11:18
  • 6
    Because then you're still searching the include paths - this way there's no searching involved. – Greg Nov 18 '09 at 12:03
  • It's a pretty simple/fast search if you place it first: `set_include_path(ROOT.PATH_SEPARATOR.get_include_path())`. Also, the maintainability could be compromised if there are alot of files to be included (elaborated my opinion more in an answer.) – chelmertz Nov 18 '09 at 12:10
  • Also look at autoloading (only applicable to loading classes). With huge libraries like Zend, autoloading makes the code a lot faster. – andho Feb 01 '12 at 03:39
8

I wrote a simple speed test script using microtime(true). It tests the following five including methods with one million iterations:

// Absolute path.
include('/home/ftpuser/public_html/includes/myscript.php');

// Predefined path.
define('PATH', '/home/ftpuser/public_html/includes/');
include(PATH . 'myscript.php');

// Relative path.
include('myscript.php');

// Using set_include_path().
set_include_path('/home/ftpuser/public_html/includes/');
include('myscript.php');

// Superglobal path.
include(dirname(__FILE__) . '/myscript.php');


Which gave the following results (in seconds):

    Absolute path:            263.222
    Predefined path:          263.545
    Relative path:            301.214
    Using set_include_path(): 302.396
    Superglobal path:         269.631


My opinion based on these results is to use a predefined path, because it's the fastest only surpassed by an absolute path. However, an absolute path has the drawback that it must be altered in every file when a change is necessary.

Hope this helped. :)

P.S.
define and set_include_path() were used only once during execution of the script (they are located outside of the loop).

Aram
  • 348
  • 4
  • 10
8

This is the best method for 99% of cases:

include(dirname(__FILE__)."/includes/myscript.php");

This ends up being an absolute path, which means it will ignore include_path, which is a known source of a large number of include related bugs in my experience...

Performance wise though, I doubt there's much difference between absolute and relative paths. This is a sort of micro optimisation that means nothing in the long run. There will generally only be 2-3 things in include_path unless you add more in. The two usual culprits are ./ and the path to wherever pear is installed.

Matthew Scharley
  • 127,823
  • 52
  • 194
  • 222
3

Definitely don't hard-code your paths, like option two. A good alternative is:

define('BASE_DIR', '/home/ftpuser/public_html/includes');
include(BASE_DIR . '/myscript.php');
include(BASE_DIR . '/myscript2.php');
include(BASE_DIR . '/myscript3.php');
include(BASE_DIR . '/myscript4.php');

Considering you'll probably have somewhere between 5 and 50 includes (I'm guessing), I wouldn't really worry about it. Just go with relative paths. The include time difference won't even be noticable. If you're developing a big web application and will have hundreds, that might be another story...

brianreavis
  • 11,562
  • 3
  • 43
  • 50
2

I tend to setup my include directories / libraries by setting the include path on the initialisation of my app.

set_include_path("/home/ftpuser/public_html/includes");
include("myscript.php");

The zend framework does something similar to load the library classes.

Andrew
  • 9,967
  • 10
  • 64
  • 103
1

when not using an absolute path, php tries to find the file in all include paths until it finds a match.

as many include paths can be added as you like, so this could , in rare cases, cause the script to be slow.
If you are including many files (for instance to initialize a framework) using absolute paths could speed up the script a little...

I think it could also cause complications when the same relative path/filename pare occur multiple times on the filesystem, and thus php selects the first occurence, when you might need another occurence

NDM
  • 6,731
  • 3
  • 39
  • 52
1

The most important thing is to arrange the include paths so that the largest amount of require/include-calls are trapped in the first mentioned path when not including a file via an absolute path in the first place.

Relying on including everything via an absolute path is hard to maintain because changing the path of your library means individually changing all those files refering to it instead of changing the include path in one place.

chelmertz
  • 20,399
  • 5
  • 40
  • 46
0

It would be good for you to test all methods by checking the time it takes to execute each, personally I have never worried about it and just used relative paths.

I guess absolute paths would be slightly faster, it might be worth wondering what happens in an error, will it spit out your full file path to the users screen (obviously turn error_reporting off) and will this cause a security risk?

Ben Everard
  • 13,652
  • 14
  • 67
  • 96