www.webdeveloper.com
Results 1 to 11 of 11

Thread: reading a file on same server

  1. #1
    Join Date
    Sep 2006
    Posts
    642

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

    Method 1:
    read file using relative path

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


    Method 2:
    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

  2. #2
    Join Date
    Feb 2012
    Location
    Tallahassee, FL
    Posts
    280
    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

  3. #3
    Join Date
    Sep 2006
    Posts
    642
    oh ok got what I was looking for...


    thanks

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,150
    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.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  5. #5
    Join Date
    Feb 2012
    Location
    Tallahassee, FL
    Posts
    280
    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:
    PHP Code:
    <?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 used to generate file being read:
    PHP Code:
    <?php
    $content 
    "";
    for(
    $x=0;$x<1000000;$x++){
        
    $content .= sha1($x) . "\n";
    }

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

    fputs($f$content);

    fclose($f);

    echo 
    "Done";
    ?>
    Last edited by NoEffinWay; 08-01-2013 at 09:38 AM. Reason: Clarification

  6. #6
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,150
    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 :-

    PHP Code:
    <?php
    $start 
    microtime();
    $var file_get_contents('H:\Program Files\Abyss Web Server\htdocs\demo.txt');
    $end microtime();
    echo 
    "Time to open 40MB file with fixed path: " . ($end-$start) . "ms";
    ?>
    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
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  7. #7
    Join Date
    Feb 2012
    Location
    Tallahassee, FL
    Posts
    280
    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):

    PHP Code:
    <?php
    $path
    [] = 'http://localhost:81/{hidden}/demo.txt';
    $path[] = 'demo.txt';
    $path[] = 'C:\OCPOS\htdocs\{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) . "ms\n";

        unset(
    $start,$end,$c);
    }
    ?>

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,148
    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.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,150
    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.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  10. #10
    Join Date
    Feb 2012
    Location
    Tallahassee, FL
    Posts
    280
    Im not sure about \\.\ here but my tests include a URI accessed file. (As shown in my second set of tests)

  11. #11
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,150
    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.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 3 users browsing this thread. (0 members and 3 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles