1

I have a function that needs to include a file, however this functions is used in a place from 200 to 300 times, this is obviously causing efficiency issues, is there a way to optimize this inside the function? (i know there are many ways in which i can fix this but it will cause too much impact in the whole application)

I will just put a little example, this is not the whole function.

function getString(arrayName, strValue){
    inclue('stringArrays.php');
    return $$arrayName[strValue];
}

I tried using include_once, but that doesn't do the job either.

Thanks in advance.

6
  • What efficiency issues ? inclusion won't affect the efficiency more than having the code in the same file. No matter you call it how many times Commented Sep 28, 2010 at 15:53
  • 1
    Do you mean that getString() is called 200 to 300 times (the include shouldn't happen more than once in this case) or that you have 200 to 300 different functions, each of which includes stringArrays.php (which is a serious performance issue)? Commented Sep 28, 2010 at 15:58
  • @Stewie: It makes the script slower since PHP has to go get the file and include it, sure if it happens 50 to 100 times no problem, but when you do it 700 times (like it is happening) you can see the problem. Commented Sep 28, 2010 at 16:11
  • @Mark: It is a function, there's no way to make the file included only once, or form the outside without making every var inside stringArrays.php global, which will cause a bit impact on the app. Commented Sep 28, 2010 at 16:13
  • I don't think so, anyways instead of file name ONLY, provide a full path OR set correct path in set_include_path(); THis way php won't have to sniff around looking for files. Commented Sep 28, 2010 at 16:17

7 Answers 7

1

You could use a static variable in the function to hold your values:

function getString($arrayName, $strValue){
static $string_arrays = array();
if (empty($string_arrays)) {
    include('stringArrays.php');
    $string_arrays = array_diff_key(get_defined_vars(), array(
        'string_arrays' => true,
        'arrayName' => true,
        'strValue' => true,
    ));
}
return $string_arrays[$arrayName][$strValue];

}

Should only include the file once.

Sign up to request clarification or add additional context in comments.

Comments

0

You could always add another parameter, perhaps a boolean, to tell the function whether or not to include it.

function getString(arrayName, strValue, includeFile)
{ 
    if (includeFile)
    {
        inclue('stringArrays.php');
    }

    return $$arrayName[strValue]; 
}

1 Comment

Everytime i call the function i need to include the file, otherwise i can't get any values from it.
0

You can try globalizing what's in stringArrays.php so you can check to see if that global variable is already set before including the file. Hard to tell without seeing what structure is in stringArrays.php.

3 Comments

Yeah that could work if it was done at the beggining of the development, however at this stage is kinda annoying.
Oops didn't mean to submit it. In stringArrays there are just different arrays with many key=>Value, however there's no big array containing everything, every array is different. $values_1=array( 'test' => 'This is a Test' ); Right now it has about 200 arrays.
You can change the stringArrays.php to $GLOBALS['values_1'] = array(... thus insuring when you include it from the function it will be set globally and can be read from all other calls of that function. You'd have to access it via: return $GLOBALS[$arrayName][$strValue]; but you can then easily check if(!isset($GLOBALS['values_1'])) inclue('stringArrays.php');
0

If your function does nothing more than include a file you should be first evaluating whether that function should be called in the first place or make the function determine if an include is required. Basically don't blindly include a file if you truly don't need it included. include_once will incur a performance hit.

1 Comment

No, it not only includes a file, like i said that was just a little example from the whole function, i just typed the basic idea.
0

Install APC, eAccelerator, XCache or any other code accelerator so PHP doesn't need to retrieve the include file from disk every time it's called. Code accelerators save the file in shared memory. That will improve performance significantly.

Comments

0

Is there anything preventing you from wrapping your current "bunch" of arrays in an array, then passing that wrapper array into the function by reference? You can then do a single require/include outside of the function. Alternatively, you can wrap both the set of arrays and the function inside an object, again bringing you down to a single require/include.

1 Comment

We'll have to transform arrays from $arrayName to $bigArray['arrayName'] yes it's a solution, but it's too much work, thanks for the help.
0

If stringArrays.php is simply a collection of arrays, what about creating a stringHandler singleton that includes stringArrays.php within the constructor and maps the each array to a class property, then a simple method to get whichever you want from that class. Then your getString() function simply references a getter method in the stringHandler.

stringArrays.php

<?php

$abc = array('def' => 'Hello',
             'ghi' => ' '
            );

$jkl = array('mno' => 'World',
             'pqr' => '.'
            );

?>

stringHandler.php

<?php

class stringHandler
{
    private static $instance;

    private function __construct()
    {
        include('stringArrays.php');
        foreach(get_defined_vars() as $key => $val) {
            $this->{$key} = $val;
        }
    }

    public static function singleton()
    {
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }

        return self::$instance;
    }

    public function getStringFromArray($arrayName, $strValue)
    {
        return $this->{$arrayName}[$strValue];
    }

}

function getString($arrayName, $strValue){
    return stringHandler::singleton()->getStringFromArray($arrayName, $strValue);
}


echo getString('abc','def');
echo getString('abc','ghi');
echo getString('jkl','mno');

?>

Kludgy, but shouldn't be a big performance overhead.

1 Comment

Yeah i agree with you, this is a better approach specially for an objec oriented application, however, we didn't do it and now it's a mess, in future applications we'll use this, right now sadly we can't but thanks for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.