About   -   Contact   -   Purchase   -   Search   -   What's New

Design Recursive Functions for Speed
I have been working on a new release of PBBrowse which utilises the new treeview control in PB5. One function that I wrote builds a hierarchical view of your application. To do this I needed to walk the tree to locate the ancestor. I wrote a function that given a label will walk recursively through the tree looking for the given label.

The function worked fine and as was defined as:

long findnode( treeview atv_Tree, long al_TVIStart, string as_Label )

The problem I had was speed, so I looked at the Powerscript in the function but I could not find any way of making it faster. Then I thought about the overhead of recursively calling the same function and all the memory allocations/de-allocations for the arguments.

So I added two attributes the the nonvisual object that contained the function. One for tree and one for the label. Then I created a public function and a protected function defined as such:

public long findnode( treeview atv_Tree, string as_Label )
protected long findnode( long al_TVIStart )

The public function loads the two attributes into the instance variables and then just calls the protected function that does all the work. I ran my code with the new function installed and found that the code to build the tree ran about twice as fast as before.

So the moral of the story is that when you are using heavily called recursive functions that it is faster to load the static function arguments once and have two functions to do the work.


Top of Page

Legal Notice

Ken Howe 2011