On Wednesday 09 November 2005 5:06 pm, Anders Johansson wrote:
On Tuesday 08 November 2005 22:41, Andre Truter wrote:
OK, I'll agree that something written in assembler is about always faster that written in any other language
Today, I would say almost never (though some extremely performance requiring programs use assembly for a few inner loops here and there)
Modern optimization technology will almost always outperform humans I would agree with you Anders as an assembler programmer. When programming in assembler, while you have the power, most of the time "you can't see the fortest for the trees". Today's compilers do a very good job not only optimizing code, but also scheduling code. Code scheduling is a form of optimization for hardware when one knows the latencies between instructions. Some optimizations might also be counter-intuative: Take this loop: if (test is false) branch to end. begin loop ... perform loop if (test is true) branch to begin end
You might code this as:
while (test is true) {
... perform loop
}
Why would the compiler insert the second test at the end of the loop?
One reason is that the beginning of the loop may reside on a different
memory page. Code were as written as below, on the last iteration, you
branch to the initial test, then since the test is false, you branch back.
This could cause 2 potential paging operations. So by inserting the second
test you avoid a branch.
start
If (test is false) branch to end.
begin loop
... perform loop
branch to start
end
Even if there is no potential page fault, on modern processors, they can
load up some of the instructions in the pipeline, and if the branch is
taken dump them. The compiler then makes some decisions based on branch
prediction.
--
Jerry Feldman