Setting output port states when looping -- what is preferred code?

If I have a loop where I will change a port state based on a condition, should I avoid issuing a (digital/analog)Write command if the condition does not change what I want the state to be? By pseudo code example, do I want to do ex. 1 or ex. 2?

ex. 1:

loop
{
(calculate value of A)
analogWrite(5,A)
}

ex. 2:
loop
{
(calculate value of A)
if LastA<>A then {
analogWrite(5,A)
LastA=A
}
}

Ex. 1 has less code but will execute the analogWrite(5,A) command even if the value of A is what port 5 is already set at.
Ex. 2 has more code but will avoid the analogWrite command unless it needs to be changed because A changed.

Which is considered "best practices" in the Arduino world?

Thanks
GB

I don't think it will make much or any difference. Setting a processor register to some value it already has should have no effect. As far as I can see from the source the analogWrite just sets the appropriate bit to enable PWM output, plus sets the duty cycle. I don't see why any sort of glitch should occur if there is no change.

And for digitalWrite it shouldn't matter either. It would be pretty weird of writing a HIGH value actually toggled the port from HIGH to LOW and back to HIGH.

However, both analog and digital writes do a bit of mucking around looking up converting pin numbers to port numbers, so they aren't the fastest routines in the world. If speed matters then bypassing the change would save a few microseconds. On the other hand, maybe you want the speed to be consistent, whether or not the pin changes value, in which case you are better off not doing the test.