views:

225

answers:

4

Is there a performance difference between these two pieces of code? My gut feeling is that the second option is slower, as the Cell object has to be constructed each time, but I like the idea of returning a Cell.

Option One:

//Call to method
initiTextDefaultCell(borders);
iTextTable.setDefaultCell(iTextDefaultCell);
//Other code...

private void initiTextDefaultCell(boolean borders) {
  if (!borders)
    iTextDefaultCell.setBorder(Rectangle.NO_BORDER);
  else 
    iTextDefaultCell.setBorder(Rectangle.BOX);
}

Option Two:

//Call to method
iTextTable.setDefaultCell(initiTextDefaultCell(borders));
//Other code...

private Cell initiTextDefaultCell(boolean borders) {
  Cell iTextDefaultCell = new Cell();
  if (!borders)
    iTextDefaultCell.setBorder(Rectangle.NO_BORDER);
  else 
    iTextDefaultCell.setBorder(Rectangle.BOX);
  return iTextDefaultCell;
}

Thanks!

+18  A: 

Write a test program and see for yourself.

Artelius
+2  A: 

The second one is (probably) slower (but see Mnementh's comment). It's doing everything the first one is doing and more. But that doesn't mean you shouldn't use it if you think it's the better design (and I tend to agree). As Lou said, this probably isn't your bottleneck. If you need to know for sure, test.

Matthew Flaschen
Don't be so sure. The first one is the same code, with the creation of a variable iTextDefaultCell ommitted in the snippet (but it is clearly needed for the code to run). So both options do the same commands, but are different structured. main difference is, that the first option uses a variable, that can be seen by the method. The second one instead creates this object itself and returns it. Maybe it will be optimized to the same native code anyway. But nobody can say without testing it.
Mnementh
You're assuming iTextDefaultCell only needs to be created for option 1. The creation's not shown for either, so it's reasonable to think it's needed either way (perhaps later).
Matthew Flaschen
+4  A: 

As you've said option two will be slower due to the allocation of a new object.

This looks like a clarity-of-code vs. performance decision. I personally think option two is clearer and that the performance impact would be negligible.

However without knowing what iTextTable is it's hard to say for certain which to use. If initiTextDefaultCell is called once when the table is instantiated then I'd go with option two, however if the number of times initiTextDefaultCell is called depends on the size of the table then option one would be better (assuming that the default cell was instantiated as part of the instantiation of iTextTable).

Nick Holt
+3  A: 

hmmm... which is slower, 10ms, or 1ms?

Option 2 will be slower than Option 1, yes, but even though the difference might be a factor of 10, even that "high" factor, when the slow version is sufficiently fastwill not be noticed by a human (The numbers used are just given as examples).

The first rule of performance is, only optimise when you have to. The second rule of performance, is a well designed system will typically have good performance, and be easier to optimise when required.

Option 2 is a lot more readable, and a better design. That is because the method creates the Cell configures it as well.

Michael Wiles