Forum Moderators: coopster

Message Too Old, No Replies

Recursive algorithm

Is there any way to optimize this function?

         

asantos

11:45 pm on May 17, 2006 (gmt 0)

10+ Year Member



Hi,
I developed a function that returns the section trace of a specific
section in a website. For example:

If section ABOUT US has the id_section = 18, then
get_seccion_trace(18) should return:

$arr[0] = 'About Us'
$arr[1] = 'Our Company'
$arr[2] = 'Start'

Then, i use something like this to make it legible for the final user:
$arr = array_reverse($hash);
//this prints Start > Our Company > About Us
echo implode(' » ',$arr);

Till know everything works fine. I was just wondering: Is there a any
way to optimize the function? I dont know if the -while loop- could
consume less resources with another recursive algorithm ... Here's the
code:

function get_seccion_trace($id) {
global $cnn;
$trace = array();
$sql = 'SELECT * FROM section WHERE id_section='.q($id);
$rs = &$cnn->Execute($sql);
while(!$rs->EOF) {
$trace[] = $rs->fields['title'];
$sql = 'SELECT * FROM section WHERE
id_section='.$rs->fields['id_section_parent'];
$rs = &$cnn->Execute($sql);
}
return $trace;
}

Thanks,
Andres S.
Gye, Ecuador

Lord Majestic

12:11 am on May 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The slowest part in your code will be SQL query - comparing to it recursive function call or while loop won't be significant.

asantos

12:27 am on May 18, 2006 (gmt 0)

10+ Year Member



so you're saying i couldnt be more optimized than that?

ergophobe

4:00 pm on May 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just curious, but
- when SELECT * are you returning unnecessary fields?
- do you need to return by reference?

asantos

4:20 pm on May 18, 2006 (gmt 0)

10+ Year Member



ergophobe:

>> when SELECT * are you returning unnecessary fields?
thanks, i already removed the unnecesary fields.

>> do you need to return by reference?
what do you mean by -reference-?

ergophobe

4:31 pm on May 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



See [us2.php.net...]

Waht I'm asking in the simplest terms is why are you doing this:

$rs = &$cnn->Execute($sql);

Instead of this

$rs = $cnn->Execute($sql);

asantos

4:49 pm on May 18, 2006 (gmt 0)

10+ Year Member



because the $cnn variable has been already instantiated at the top of the page, so im re-using it.

that's what the & is for, right?

jatar_k

4:51 pm on May 18, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



no, read the page ergophobe linked to and you will be able to learn all about references

you don't need the & in this case from what I can tell

asantos

4:59 pm on May 18, 2006 (gmt 0)

10+ Year Member



Ok, ill go read the page. But just as a comment, the ADOdb PHP Library had the & in the DB connection example.

ergophobe

5:41 pm on May 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It could well be that the ADODB abstraction layer needs to work that way, or it could be that the & is used to boost performance. In the case of PHP4 there is still some advantage to using references with copies of objects and that may be why they return a reference here, because the function does return an object. In PHP5 that performance gain is lost as objects are passed and assigned like any other variable, so the performance effect would depend a bit on how large the object is and what version of PHP you're using.

As of PHP4, PHP uses a system called "reference counting [zend.com]" which means that by default variables are passed by reference and assigned by reference, with copies only being made once a new value is assigned to one of the references. The except to this is with objects, so when you assign

$rs = &$cnn->Execute($sql);

In PHP4 you are making a copy of the object returned by the Execute() function. As I mentioned, in PHP5, objects behave like any other variable, so no copy will get made until you try to modify one object or the other. Since you probably don't have access to the original object within $cnn (unless it's assigned to $this->rs before getting returned), there's not much worry there. So in PHP5 the reference operator is just confusing and gives no performance boost.

If you are using PHP4, you may get a significant or insignificant performance bump depending on the nature of the $rs object.

There's no performance loss by using the & in either case, but in the name of performance you may have inadvertently set up a variable alias to the $cnn object and that could get confusing, but probably not if the $rs gets thrown away relatively quickly and you never attempt to modify it or assign values to it.