To view parent comment, click here.
To read all comments associated with this story, please click here.
acobar,
"Well, I will repeat to you what I do to everyone I met and that would ask my advise (not your case) on programming: first look for a good, tested, implementation. Many people try to code fragments that are already optimized for ages. They don't look at Donald Knuth's ACP, fftw, lapack, atlas and many other well-thought code before they start and, as such, write subpar code."
I actually don't encourage people to optimize things at such small levels or in assembly. I'm just disturbed that so many people are under the false impression that their compilers are so difficult to beat.
"You get a pretty decent optimized code:"
Yes, I am aware that we can coerce the compiler to generate better instructions by rewriting the C code.
(Although it still isn't as good for the other reasons already discussed).
It concerns me that GCC converts our C code to assembly so literally instead of generating more optimal paths.
Should we really encourage devs to adjust thier programming style to compensate for GCC optimizer deficiency?
Does this mean that C devs will have to understand CPU internals in order to have GCC produce optimal code?
I really don't think we should be making excuses for GCC here. There is nothing unreasonable about expecting the original code to be optimized by the compiler.
Edit: By the way I am guilty of rewriting C code at the micro level in order to help GCC optimizer along. But we shouldn't have to do this in the first place.
Edited 2011-06-11 10:08 UTC
Perhaps, I should have said it on a more direct style: I do not encourage people to write optimized code, I tell them to look for good algorithms and rather to good libraries whenever they are available.
To me, actually, all this talk about the better language to code in is a bit misleading, we should clearly concentrate on algorithms. We don't know on what we will be coding 20 years from now be we know that good algorithms will probably survive, like they did on past.
Sincerely, for most of the applications it does not matter a dime if you spend 1 millisecond or 1 microsecond doing something, and there are a factor of 1000 involved. And for the ones that matter, there are a chance that it is already well known and that a wise compromise was devised.
I see people everyday praising languages that make them write less code, or type less, or write more comfortable (understandable) code, or in the ones they like the syntax more. And all those arguments are sensible.
I, particularly, like languages that have all tools I feel I may need, like GUI generators, automation by scripts, code templates (not necessarily like on C++), profilers (for the very rare occasions where they are needed), many of modern OO concepts and, of course, that may be linked against good, well known tested libraries.
To be even more direct: people should learn more before code, unlike what we so repeatedly see. This would save time on rewrites and latter tunes.
Edited 2011-06-11 11:38 UTC
I'd argue that the C/C++ developer shouldn't have to do black magic in order to optimize its code either.
Alfman's use of for loops is perfectly normal, easy to understand, and you'll find it in every book on C. One shouldn't have to sacrifice code clarity like you did in order to get better performance, especially on such trivial code.
Sorry, but I fail to find where the code I showed is "less understandable". It is actually a very well known technique, i.e., reduce the number of unneeded comparisons.
Anyway, it was used only to point out one of the many gross assumptions people do about code efficiency, compiler optimizations and related stuff. We clearly should teach more about algorithms than about languages like we do today.
If you are not a wizard you should not be programming C/C++. There are other languages that are much better suited for high-level programming. C/C++ are only superior languages in enabling more power to a powerful wizard, in unskilled hands they are at best teaching/testing tools.
i--;
a[i] += b[i];
}
I'm pretty sure this is a terrible idea because it will mess up all the cache lines and pre-fetching if you walk through memory backwards. "
No. Prefetchers (in every processor I've touched) support negative strides and working backwards have no performance problems.





Member since:
2005-11-15
Well, I will repeat to you what I do to everyone I met and that would ask my advise (not your case) on programming: first look for a good, tested, implementation. Many people try to code fragments that are already optimized for ages. They don't look at Donald Knuth's ACP, fftw, lapack, atlas and many other well-thought code before they start and, as such, write subpar code.
For example, if you change your code to:
void vector_add(unsigned int*a, unsigned int a_len, unsigned int*b, unsigned int b_len)
{
unsigned int i;
for (i = a_len < b_len ? a_len : b_len ; i > 0 ; ) {
i--;
a[i] += b[i];
}
}
You get a pretty decent optimized code:
vector_add:
.LFB0:
.cfi_startproc
cmpl %esi, %ecx
cmovbe %ecx, %esi
testl %esi, %esi
je .L1
.p2align 4,,10
.p2align 3
.L4:
decl %esi
mov %esi, %eax
movl (%rdx,%rax,4), %ecx
addl %ecx, (%rdi,%rax,4)
testl %esi, %esi
jne .L4
.L1:
rep
ret
.cfi_endproc
As can be clearly seen, compilers are not black-magic wizards and, regretfully, depend on the code quality we give to them, and we should never forget that.