![]() |
| ||
| array indexing vs pointer arithmetic int x[5]; Array indexing has a cleaner syntax but (as I've read) pointer arithmetic is faster. My question is, is it worth to use pointer arithmetic? Is there a noticeable difference? |
| ||
| Re: array indexing vs pointer arithmetic Quote:
|
| ||
| Re: array indexing vs pointer arithmetic Alright, thanks! :) |
| ||
| Re: array indexing vs pointer arithmetic >but (as I've read) pointer arithmetic is faster Prove it. In your example, the two are quite likely compiled to exactly the same machine code. Array subscripting is literally syntactic sugar for the equivalent pointer offset calculation. A more likely example is this: int x[N];The latter could very well be faster (though I would question why you're worried about such micro-optimizations) due to the fact that the body of the loop is only performing a dereference rather than an addition and a dereference. My advice is not to worry about it. Use whichever more clearly displays your intentions. |
| ||
| Re: array indexing vs pointer arithmetic From the C++ Standard: Quote:
int countDigits(const char* str)Sometimes we have no choice: as usually (but not always) pointer arithmetics code is faster than "subscripted" one on embedded systems where we (often) must save on every microsecond... It's a far-fetched problem. It's not a language problem... |
| All times are GMT -4. The time now is 7:04 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC