pointer arithmetic vs. arrays
Hey everyone. I've heard (and have also read in "The C Programming Language") that pointer arithmetic in place of arrays is, in general, faster. However, I've also heard that modern compilers automatically make optimizations that account for this difference.
In everyone's opinion, taking both readability and issues of speed into account, what is better to use? I'd imagine this would depend on the situation. Just trying to pick up better coding habits as I learn :)
James
Signature · 4 lines
My blog: http://www.crazydrclaw.com/ My homepage: http://james.colannino.org/ "If Carpenters made houses the way programmers design programs, the first woodpecker to come along would destroy all of civilization." --Computer Proverb
Quoted 8 lines · James Colannino
Hey everyone. I've heard (and have also read in "The C Programming Language") that pointer arithmetic in place of arrays is, in general, faster. However, I've also heard that modern compilers automatically make optimizations that account for this difference.
In everyone's opinion, taking both readability and issues of speed into account, what is better to use? I'd imagine this would depend on the situation. Just trying to pick up better coding habits as I learn :)
In most cases, I'd recommend array indexing.
If something can reasonably be expressed in either form, array indexing is usually clearer (both to read and to debug), and compilers which don't optimise array access are rare enough not to be worth worrying about.
Signature · 1 line
Glynn Clements <[email protected]>