Fog Creek Software
Discussion Board




A slightly more serious question

If I want to build a list of elements and then turn that list of elements into an XML document (or at least a branch) should I:

(a) build the list and then iterate over the list to build the XML?

or

(b) build the list and the XML at the same time?


The advantage of (a) is that the code is easier to understand and easier to unit test (smells good!).

The advantage of (b) is that you don't reloop over the list of elements.

I'm leaning with (a) - the list isn't really going to be that big, so performance seems unlikely to be a problem.

Your thoughts?

Walter Rumsby
Tuesday, December 02, 2003

I lean towards doing one complete task, then the other.  It's easier to adapt each separate procedure later if you want them to do other things, plus if you change your XML layout (and trust me, you will), you only have to touch that code.

Sam Livingston-Gray
Tuesday, December 02, 2003

O(2n) == O(n)

mb
Tuesday, December 02, 2003

and 20 days to complete computation == 40 days to complete computation!


Tuesday, December 02, 2003

I would hazard a guess that it's not the loop that hurts performance but the work performed within that loop. Thus, setting up LoopA to perform Task1 and Task2 in concert is likely to give performance indistinguishable from setting up LoopB to perform Task1 and LoopC to perform Task2.

If this guess is anywhere close to right, then I'd keep the loops separate unless there was a significant amount of actual work to be saved by 'bundling'.

Ron Porter
Tuesday, December 02, 2003

Think of the poor maintenance programmer...which will be easier to understand, to change when <X> needs to be <XX>, to debug, etc.

From your brief description I would lean to two loops -- unless you want to maintain this code forever and ever and ever.

Billy Boy
Tuesday, December 02, 2003

Walter-

Unless your list of elements is the most ungainly hierarchical mess imaginable, it's not likely to scale worse than O(n).  That's the XML DOM code's job.  ;>  (Seriously, the DOM code I've worked with has been ludicrously slow.)

Ron has it right; I can't imagine a case in which LoopA would give you a performance advantage, but I can easily predict that LoopA would waste programmer time, which is far more valuable.

Sam Livingston-Gray
Tuesday, December 02, 2003

Premature optimization is the root of all evil. 

Write it the way you feel is correct and then run a profiler. In most cases there will be no need to change code like this -- it just is not that slow as to be perceptible. 

I have no doubt that joining the two loops would make execution somewhat faster but at the cost of what? maintainability, ease of explanation, reuse, abstraction of ideas, ease of testing, commenting -- all of these items also take or cost in terms of time. 

Billy Boy
Tuesday, December 02, 2003

I take it that the List is the internal representation, and the XML version may be required.  How often is the XML required?

If both are built together it means that every time you want the list, you also have the cost of building the xml.  Considering how costly XML is compared to a list this is bad.

If you produce the list, then the XML from the list on demand then overall you'll do a lot less work.

Of course, this all depends on the useage.  10:1 where the list gets create 10 times for every 1 time the XML is required?  2:1? 1:1? 1:20?

If you can get a good estimate, then you have your answer.

Ged Byrne
Tuesday, December 02, 2003

Actually, I am the "poor maintenence programmer" in this case :)

Walter Rumsby
Tuesday, December 02, 2003

Make it work.
Make it right.
Make it fast (and then only if you decide you need to).

Gordon Hartley
Wednesday, December 03, 2003

*  Recent Topics

*  Fog Creek Home