/    Sign up×
Community /Pin to ProfileBookmark

reading a file on same server

HI

Suppose I have a php file “read_file.php” that is used to read .txt files, the structure of the folder/files are:

/www/read_file.php
/www/public/upload/

I was wondering which will be faster and why in PHP..

[B]Method 1:[/B]
read file using relative path

Eg: get_file_contents(“public/upload/test.txt”);

[B]Method 2:[/B]
read file using URL (note that the server which is making the request and the server where the file is being read is same)

Eg: get_file_contents(“http://mydomain.com/public/upload/test.txt“);

Can someone explain me which is faster and why?

Thanks

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@NoEffinWayJul 31.2013 — Relative will always be faster.

When using a URL, the script has to open a http connection to the file, download the file (in a sense) and then read the file.

Relative simply opens the file.

Here are some benchmarks I just ran.

Time to open 40MB file with URL path (Using localhost): 4.9771611690521ms

Time to open 40MB file with URL path (Using localhost): 4.1154129505157ms

Time to open 40MB file with URL path (Using localhost): 4.2288191318512ms

An extreme of: 14.318599939346ms

Time to open 40MB file with relative path: 2.2637801170349ms

Time to open 40MB file with relative path: 2.113431930542ms

Time to open 40MB file with relative path: 2.2605271339417ms
Copy linkTweet thisAlerts:
@phantom007authorAug 01.2013 — oh ok got what I was looking for...


thanks
Copy linkTweet thisAlerts:
@rootAug 01.2013 — It would be interesting to know what script was used to obtain those figures. Not all servers are equal because your server could be a dedicated rack, a virtual server which will be shared space and that impacts on delivery speed or your server may be one that is a shares server that hibernates until something other than the index page is used.
Copy linkTweet thisAlerts:
@NoEffinWayAug 01.2013 — Yeah, fair enough. It was ran on my computer that I develop on, it's a Windows machine. Apache2.2 and PHP5.3.8

Note that this script was ran from my IDE (Removing Apache from the equation all together), I did this just to show an idea of the time it would take.

Code used for Testing:
[code=php]
<?php
echo file_get_contents('demo.txt');
$end = microtime();
echo "Time to open 40MB file with relative path: " . ($end - $_SERVER["REQUEST_TIME_FLOAT"]) . "ms";
?>
[/code]


Code used to generate file being read:
[code=php]
<?php
$content = "";
for($x=0;$x<1000000;$x++){
$content .= sha1($x) . "n";
}

$f = fopen('demo.txt','w+');

fputs($f, $content);

fclose($f);

echo "Done";
?>
[/code]
Copy linkTweet thisAlerts:
@rootAug 01.2013 — I was getting strange negative figures so I modified the script and got the following

Time to open 40MB file with relative path: 0.15625ms

Time to open 40MB file with fixed path: 0.140625ms

the script was modified to :-

[code=php]<?php
$start = microtime();
$var = file_get_contents('H:Program FilesAbyss Web Serverhtdocsdemo.txt');
$end = microtime();
echo "Time to open 40MB file with fixed path: " . ($end-$start) . "ms";
?>[/code]


I had the output dumped in to a variable because it is more likely that this would be the scenario on a production server, the 40MB file was produced with the supplied script unchanged.

Unless your script is going to offer up a service that millions of people will be relying on, the minute difference of 1/100th of a second is not going to make that much difference but it will if your server is going to be serving millions of users, the 1/100th of a second difference will make a big difference.

I noted a small variance in both methods, this was approximately 3/100ths of a second over a short period of time.

That was a pretty interesting exercise and experiment and would be interesting to find others who are willing to try both scripts and see what results are gained.

My local machine used to run XAMPP but because developers have basically crippled it with silly security, I dumped it and installed Abyss (which is made by former Apache developers if you look in to the history of the departure!) and PHP 5.4
Copy linkTweet thisAlerts:
@NoEffinWayAug 01.2013 — Yeah, that is interesting. I wonder why the time is so different. This is very interesting to me. I ran it again (Note: the {hidden} is work related directories):

Time to open 40MB file with path 'http://localhost:81/{hidden}/demo.txt': 2.5665011405945ms

Time to open 40MB file with path 'demo.txt': 0.096905946731567ms

Time to open 40MB file with path 'C:{hidden}htdocs{hidden}demo.txt': 0.055022001266479ms

Time to open 40MB file with path 'http://localhost:81/{hidden}/demo.txt': 2.5337021350861ms

Time to open 40MB file with path 'demo.txt': 0.16361808776855ms

Time to open 40MB file with path 'C:{hidden}htdocs{hidden}demo.txt': 0.06325888633728ms

Time to open 40MB file with path 'http://localhost:81/{hidden}/demo.txt': 2.4332671165466ms

Time to open 40MB file with path 'demo.txt': 0.15351891517639ms

Time to open 40MB file with path 'C:{hidden}htdocs{hidden}demo.txt': 0.075954914093018ms


I think my times are so much lower this time because on the previous tests, I was running a rather heavy data calculator while running the script.

Here is the script (and again, I ran the script from the IDE):

[code=php]
<?php
$path[] = 'http://localhost:81/{hidden}/demo.txt';
$path[] = 'demo.txt';
$path[] = 'C:OCPOShtdocs{hidden}demo.txt';

foreach($path as $x => $p){
$start = microtime(true);
$c = file_get_contents($p);
$end = microtime(true);
echo "Time to open 40MB file with path '{$p}': " . ($end - $start) . "msn";

unset($start,$end,$c);
}
?>
[/code]
Copy linkTweet thisAlerts:
@NogDogAug 01.2013 — Just to be sure we're not talking apples vs. oranges here, the original question was really about the difference between using a file path versus a HTTP request via a URI. The difference between a relative file path and absolute file path is a different issue, and likely to be very minimal.
Copy linkTweet thisAlerts:
@rootAug 01.2013 — The difference I feel is minimal appart from the fixed path is fixed so any movement in the server will break the script and path therefore an absolute path vs relative will be obvious, the advantage being that with a relative path the server works out the path to the file.
Copy linkTweet thisAlerts:
@NoEffinWayAug 01.2013 — Im not sure about . here but my tests include a URI accessed file. (As shown in my second set of tests)
Copy linkTweet thisAlerts:
@rootAug 12.2013 — Yes both methods were used.

Time depends on the production server and its load.

If the host is shared or dedicated will also play a part in speed.
×

Success!

Help @phantom007 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...