Categories > TinyButStrong general >

static function f_Misc_GetFile(&$Txt,&$File,$LastFile='') suggestion

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Daniel Tsviatkov
Date: 2010-12-11
Time: 13:53

static function f_Misc_GetFile(&$Txt,&$File,$LastFile='') suggestion

Hi there. I am using this great tool for a while and its functionality and speed is incredible.
On the project i am working atm i am catching all errors and format in specific files.
In this function static function f_Misc_GetFile(&$Txt,&$File,$LastFile='') on line 3110 i got few errors, and saw it can be 'optimized' in some ways.
I am using PHP5.3 and TBS version 3.6.1
Here is my version of this piece of code.
static function f_Misc_GetFile(&$Txt,&$File,$LastFile='') {
// Load the content of a file into the text variable.
    $Txt = '';
    if(!file_exists($File)){
        if($LastFile==='') return false;
        $File2 = dirname($LastFile).'/'.$File;
        if(!file_exists($File2)) return false;
        $File =& $File2;
    }
    $fs = filesize($File); // return False for an URL
    if ($fs===false) {
        $fd = fopen($File,'r',true);
        while (!feof($fd)) $Txt .= fread($fd,4096);
        fclose($fd);
    } else {
        if ($fs>0) $Txt = file_get_contents($File);
    }
    return true;
}

The code seems to work normaly. No idea if the perfomance will be cuted, but aleast skiper few '@' operators which seems to be abit slow.

Regards Daniel.
By: Skrol29
Date: 2010-12-13
Time: 11:40

Re: static function f_Misc_GetFile(&$Txt,&$File,$LastFile='') suggestion

Hi Daniel,

Thanks for your appreciation about TBS.

Your code suggestion is better that the current one, indeed.
I'll incorporate your snippet in the next TBS version.

Nevertheless, I'm surprised that @ can be slower. I'll make some benches. The @ option helps to avoid the PHP notice message when a template cannot be read because it's locked. But this is quite rare.
By: Daniel Tsviatkov
Date: 2010-12-13
Time: 15:29

Re: static function f_Misc_GetFile(&$Txt,&$File,$LastFile='') suggestion

I know @ is for suppressing error messages, but i am using custom error handler, and even then the messages are coming.
About its performance i never tested it, but have red at many places that it slows abit the whole script.

PS. Ok after some digging found that '@' itself is not that slow, bu when an error occurs it can be twice slower, than checking for the error.
http://michelf.com/weblog/2005/bad-uses-of-the-at-operator/
Also not sure, but i think i have red somewhere on php.net that devs are trying to optimize its work, but not sure what they did at all.
By: Skrol29
Date: 2010-12-14
Time: 17:52

Re: static function f_Misc_GetFile(&$Txt,&$File,$LastFile='') suggestion

Here is the result of my benches:

[@fopen() with existing file] is 1.71 time faster than [file_exists() with existing file] , that is a reduction of -41.48% compared to [file_exists() with existing file].
[file_exists() with non existing file] is 4.04 time faster than [@fopen() with non existing file] , that is a reduction of -75.24% compared to [@fopen() with non existing file].

The bench test is available at the SVN repository at SourceForge
  trunk/benches/phpbench_fileexists_vs_fopen.php
By: Daniel Tsviatkov
Date: 2010-12-14
Time: 20:23

Re: static function f_Misc_GetFile(&$Txt,&$File,$LastFile='') suggestion

As i said, i never tested the performance. It was strange when i got the messages even if the files were found.
And to still stay on the subject found this on php.net

http://www.php.net/manual/en/function.file-exists.php#97774

[quote]
I timed is_file and file_exists, it seems like is_file() is almost 2x faster when a file exists and about the same when it doesn't.
[/quote]

So i will change the code to use is_file.
Btw couldn't find the benches you were talking about. Can you post me a link so i can have an eye on it.
The curiosity got me :)
Regards Daniel.
By: Skrol29
Date: 2010-12-14
Time: 23:12

Re: static function f_Misc_GetFile(&$Txt,&$File,$LastFile='') suggestion

By: Skrol29
Date: 2011-03-11
Time: 21:48

Re: static function f_Misc_GetFile(&$Txt,&$File,$LastFile='') suggestion

Hi,

The file_exists() tip was integrated into the current beta version of TBS. But unfortunately I will be obliged to backtrack because I've discovered an compatibility problem: fopen() does search the file in the include_path and the __FILE_path, while file_exists() doesn't.