Test Results
10,000,000 iterations of Block 1
myFlag = 0: 23.8ns per iteration
myFlag = 1: 23.8ns per iteration
10,000,000 iterations of Block 2
myFlag = 0: 23.8ns per iteration
myFlag = 1: 46.8ns per iteration
Block 2 is 96% slower than Block 1. Makes sense, since Block 2 does twice the work in the pessimistic case.
i prefer either case, depending on the situation. If
myFlagis rarely ever 1, then it want it to stand out as the edge case that we have to handle. If both are equally likely, i want theif-elsesyntax. But that’s preference, not fact.
Decades ago, the intel 80286 dual pipeline would stall if a conditional jump was taken, rather than falling through to the next instruction. By the time of the Pentium that went away; the CPU pre-fetches both branch paths. But in the back of my mind i still have a twinge of fear whenever i write code that has the most common outcome in the else clause. Every time i have to remind myself that it doesn’t matter anymore.
Int32 reps = 10000000;
private void Block1(int myFlag)
{
String width;
String height;
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < reps; i++)
{
if (myFlag == 1)
{
width = String.Format("{0:g}%", 60);
height = String.Format("{0:g}%", 60);
}
else
{
width = String.Format("{0:g}%", 80);
height = String.Format("{0:g}%", 80);
}
}
sw.Stop();
Double time = (Double)sw.Elapsed.Ticks / Stopwatch.Frequency * 1000000000.0 / reps;
MessageBox.Show(time.ToString() + " ns");
}
private void Block2(int myFlag)
{
String width;
String height;
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < reps; i++)
{
width = String.Format("{0:g}%", 80);
height = String.Format("{0:g}%", 80);
if (myFlag == 1)
{
width = String.Format("{0:g}%", 60);
height = String.Format("{0:g}%", 60);
}
}
sw.Stop();
Double time = (Double)sw.Elapsed.Ticks / Stopwatch.Frequency * 1000000000.0 / reps;
MessageBox.Show(time.ToString() + " ns");
}
String.FormatmakesIF96% slowerGetPercentageString(0.60)makesIF96% slower
const
reps = 10000000;
procedure Block1(myflag: Integer);
var
width, height: string;
i: Integer;
t1, t2: Int64;
time: Extended;
freq: Int64;
begin
QueryPerformanceCounter(t1);
for i := 1 to reps do
begin
if myFlag = 1 then
begin
width := '60%';
height := '60%';
end
else
begin
width := '80%';
height := '80%';
end;
end;
QueryPerformanceCounter(t2);
QueryPerformanceFrequency(freq);
time := (t2-t1) / freq * 1000000000 / reps;
ShowMessage(FloatToStr(time)+ 'ns');
end;
procedure Block2(myflag: Integer);
var
width, height: string;
i: Integer;
t1, t2: Int64;
time: Extended;
freq: Int64;
begin
QueryPerformanceCounter(t1);
for i := 1 to reps do
begin
width := '80%';
height := '80%';
if myFlag = 1 then
begin
width := '60%';
height := '60%';
end;
end;
QueryPerformanceCounter(t2);
QueryPerformanceFrequency(freq);
time := (t2-t1) / freq * 1000000000 / reps;
ShowMessage(FloatToStr(time)+ 'ns');
end;
Doing twice the amount of work takes roughly twice the amount of time.
Answer: IF does not perform better than IF-ELSE.
