Interface Implementation causing performance issues
Posted on 2010-09-17
I have refactored a section of code that originally looped through a bunch of objects (1 loop, all the objects where the same object type called "Element").
The new code I have refactored now has implemented advanced object oriented methods, I now have 2 classes that each implement a single interface called ISearchComponent.
the ISearchComponent has a method called "search".
the two classes that implement the interface are
NodeComposite (this holds a List of ISearchComponents, each item is looped through when "search" is called and search is called on each of the items in the list)
LeafComponent (this object is responsible for determining if its true or not, ie, does this text appear in a document?)
A search can consist of multiple nodes and leafs in a large hierarchy, this new refactored code is also highly optimised in terms of business logic because of the way its built, if certain boolean criteria are not fulfilled (ie one Leaf under a "AND" NodeComposite returns false, then it immediately escapes out of the loop and returns false) and so forth, so logically, you would think that this should be much much faster.
It turns out, whatever the problem is, its not, in fact its 4 times slower. Running the same search on the old version runs in 1 minute 6 seconds, on the new version is runs at 4 minutes 30 seconds, an obviously huge performance difference.
In terms of the code, it does exactly the same as the previous, except now its structured in a tree, there are more than 1 loop because its recursive and theres 2 classes and an interface.
Could it actually be the case that because I have used interfaces and a composite design pattern that its made it slower?