The jmp family of instructions should be avoided whenever possible. They will cause the pipeline to stall. On x86 architectures, especially the new PIII and PIV CPUs, these and cache misses are probably the two largest single performance hits. In your code, Sayeh, you do a couple of things that add unnecessary jumps and therfore slow down the code unnecessarily. For example, you frequently do the following:
goto Cleanup;
What is at Cleanup?
goto Done;
So essentially you've got two jumps, neither of which are necessary. The best way to exit a function is not to jump to the end of it, but to return. You do know, I hope, that you can exit a void function with a simple "return;" command!
Also, you say that using gotos eliminate tests. However, if all you do with gotos is jump around code in the same manner regardless of circumstances, you might as well just write your code in that order! Gotos are almost invariably used in conjunction with IF statements. This is in fact what you did. This is inefficient. Why? Let's look at the following code:
Code:
if(x == 2)
{
x++;
}
else
{
x--;
}
This will break down into ASM something like this:
Code:
mov eax, x
cmp eax, 2
jne notequal
inc eax
jmp done
notequal:
dec eax
done:
mov x, eax
Using your method, however, you write C++ code like this:
Code:
goto start
cleanup:
goto done;
start:
if(x == 2)
goto xisequal;
else
goto xnotequal;
xisequal:
x++;
goto cleanup;
xnotequal:
x--;
goto cleanup;
done:
This code will break down into the following:
Code:
jmp start
cleanup:
jmp done
start:
mov eax, x
cmp eax, 2
je xisequal
jmp xnotequal
xisequal:
inc eax
jmp cleanup
xnotequal:
dec eax
jmp cleanup
done:
mov x, eax
Not only are the latter C++ and ASM versions longer and slower; they're harder to read.
I sympathize with your disallusionment with acedemic programming instruction, Sayeh, but sometimes what is said is indeed practical. There may, I admit, be times when GOTO should be used. The hard-and-fast rules of professors can conflict with the (ideally) more important rule of efficiency. But in this example they do not. Academics say you should avoid GOTO; in this instance, they're right.