0

I made a PHP app which was taking about ~0.0070sec for execution. Now, I added a hashtable array with about 2000 values. Suddenly the time for execution has gone up to ~0.0700 secs. Almost 10 times the previous value.

I tried commenting out the part where I was searching inside the hashtable array (but array was still left defined). Still, the execution time remains about ~0.0500secs.

Array is something like:

$subjectinfo = array(
        'TPT753' => 'Industrial Training',
        'TPT801' => 'High Polymeric Engineering',
        'TPT802' => 'Corrosion Engineering',
        'TPT803' => 'Decorative ,Industrial And High Performance Coatings',
        'TPT851' => 'Project');

Is there any way to optimize this part?

I cannot use Database as I am running this app on Google app engine which is still not supporting JDO database for php.

Some more code from the app:

function getsubjectinfo($name)
    {
        $subjectinfo = array(
        'TPT753' => 'Industrial Training',
        'TPT801' => 'High Polymeric Engineering',
        'TPT802' => 'Corrosion Engineering',
        'TPT803' => 'Decorative ,Industrial And High Performance Coatings',
        'TPT851' => 'Project');

    $name = str_replace("-", "", $name);
    $name = str_replace(" ", "", $name);
    if (isset($subjectinfo["$name"]))
        return "(".$subjectinfo["$name"].")";
    else
        return "";
   }

Then I am using the following statement 2-3 times in the app:

echo $key." ".$this->getsubjectinfo($key)
14
  • Are you constructing this array only once in your program? Commented Mar 10, 2011 at 14:08
  • yes. This array will remain contant for weeks... Commented Mar 10, 2011 at 14:09
  • @hiprakhar: That's not what I asked. Commented Mar 10, 2011 at 14:10
  • @jon I have declared this array only once in my project. Searching inside this array 2 times throughout the app lifetime. Commented Mar 10, 2011 at 14:12
  • If every key has TPT in it, try to make the array with integer indices without TPT. Integer indices will work faster Commented Mar 10, 2011 at 14:19

4 Answers 4

1
function getsubjectinfo($name)
{
    $subjectinfo = array(
    'TPT753' => 'Industrial Training',
    'TPT801' => 'High Polymeric Engineering',
    'TPT802' => 'Corrosion Engineering',
    'TPT803' => 'Decorative ,Industrial And High Performance Coatings',
    'TPT851' => 'Project');
// ..
}

This way the array is created everytime you call the function. Consider using a static variable here

function getsubjectinfo($name)
{
    static $subjectinfo = array(
    'TPT753' => 'Industrial Training',
    'TPT801' => 'High Polymeric Engineering',
    'TPT802' => 'Corrosion Engineering',
    'TPT803' => 'Decorative ,Industrial And High Performance Coatings',
    'TPT851' => 'Project');
// ..
}

And as a sidenote: You can also use a SQLite-Database :)

Update: An OOP approach

class MyClass {
    public static $subjectnames = array(
      'TPT753' => 'Industrial Training',
      'TPT801' => 'High Polymeric Engineering',
      'TPT802' => 'Corrosion Engineering',
      'TPT803' => 'Decorative ,Industrial And High Performance Coatings',
      'TPT851' => 'Project');

    public function getsubjectinfo($name) {
        $name = str_replace("-", "", $name);
        $name = str_replace(" ", "", $name);
        if (isset(self::$subjectnames["$name"]))
            return "(".self::$subjectnames["$name"].")";
        else
            return "";
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@kingchurch Thanks! I am doing almost the same by using constructor to define the array.
Dont see any constructor and it would not change anything: $subjectinfo is a local variable and will get created in a method everytime its call too.
0

You could try using integers as keys into your table.

"But all keys do not necessarily have TPT, they may be TOE362, AGS612 etc. I have sorted them in ascending order, but wonder if it helped."

Hash your original strings into numeric data, and use that output as your hash keys. Yes, there is a constant-time penalty involved per access (for the extra hash), but if your final data set is large enough, I suspect this may outperform letting PHP use string keys directly.

If all else fails, write the performance sensitive code in C and compile it as a PHP extension. No, better yet, write the whole app in C. Better yet, use direct machine code for everything. Or wire a breadboard with your desired logic! Reference: http://xkcd.com/378/

1 Comment

Thanks the comic strip.. liked it :)
0

Lesson of the day learnt- Defining large arrays in the class constructor can speed up the things.

I modified the code as:

class myclass
{
    var $subjectnames = array();
    function myclass()
    {
        $this->subjectnames = array(
    'TPT753' => 'Industrial Training',
    'TPT801' => 'High Polymeric Engineering',
    'TPT802' => 'Corrosion Engineering',
    'TPT803' => 'Decorative ,Industrial And High Performance Coatings',
    'TPT851' => 'Project');
    }


function getsubjectinfo($name)
    {
        //$subjectinfo = array();
        $name = str_replace("-", "", $name);
        $name = str_replace(" ", "", $name);
        if (isset($this->subjectnames["$name"]))
            return "(".$this->subjectnames["$name"].")";
        else
            return "";
    }
}

Comments

0

I once read that you can optimize such a static array configuration via serialize/unserialize.

Inject in your source code the serialized version of the array. It will be a string.

Use unserialize(..) to dynamically build the array.

Urban legend says It might save some parsing time.

another thing you can try is to use object properties instead of array keys.

$obj->TRV3463 may be faster than array access.

Comments

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.