Archived

This topic is now archived and is closed to further replies.

nano

iteration over arrary

Recommended Posts

Which iteration method comsumes more cpu? Iteration with the [] operator or incrementing a pointer?

Share this post


Link to post
Share on other sites
By incrementing a pointer, I simply do an addition and
a value from mem with *, right?

For the [] operator you''d have to do something like
a[n] = *(a+sizeof(T)*n), and that''s a multiplication
operation of a difference, or?

To me it seems that [] takes more clock cycles, but
i don''t knwo how it''s asmd so I wouldn''t know.

Share this post


Link to post
Share on other sites
quote:
Original post by nano
For the [] operator you''d have to do something like
a[n] = *(a+sizeof(T)*n)

The guys who wrote your compiler are well aware of that.

Share this post


Link to post
Share on other sites
quote:
The guys who wrote your compiler are well aware of that.


If this is all you wanted to say then why the h**l did you post?

Share this post


Link to post
Share on other sites
Nano, your second-to-last post betrays a lack of understanding about how i386 opcodes can express memory addresses. The multiplication and addition can both be performed inline with the memory access.

I suggest you take 3 minutes to actually write two programs, one that uses pointer incrementing and one that uses index incrementing, and time each one.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
I was just wondering. Compilers do a lot of optimizing and modern cpus do all kinds of crap too so I''m kind of curious.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by nano
Compilers do a lot of optimizing and modern cpus do all kinds of crap too
That''s why the best way is to try it

Share this post


Link to post
Share on other sites
Well, I timed them and foun that on P3 and Athlon,
compiled with vc, the operator takes 0.99 of the
time it takes for a pointer increment.
Weird. I thought that incrementing a pointer
saved some instructions, but I guess I was wrong.

Share this post


Link to post
Share on other sites
quote:
Original post by nano
Well, I timed them and foun that on P3 and Athlon,
compiled with vc, the operator takes 0.99 of the
time it takes for a pointer increment.
Weird. I thought that incrementing a pointer
saved some instructions, but I guess I was wrong.



That is, 99%? In my experience that means the same. Run it again/use another compiler/use another computer and you might get 98%, or 101%. Maybe even a couple more percent difference than that. In fact, I think some compilers are capable of optimizing them into the same thing. Basically, don''t worry about such a small difference. The common wisdom is "optimize by changing the algorithm, let the compiler handle the micro-optimizations".

Share this post


Link to post
Share on other sites
quote:
That is, 99%? In my experience that means the same. Run it again/use another compiler/use another computer and you might get 98%, or 101%. Maybe even a couple more percent difference than that. In fact, I think some compilers are capable of optimizing them into the same thing. Basically, don''t worry about such a small difference. The common wisdom is "optimize by changing the algorithm, let the compiler handle the micro-optimizations".


As I said earlier, I was just curious. I know it''s useles
to be thinking of such tiny details when the compilers
will probably always do better that me.
It''s still always interesting to know.

Share this post


Link to post
Share on other sites
quote:
Original post by nano As I said earlier, I was just curious. I know it''s useles
to be thinking of such tiny details when the compilers
will probably always do better that me.
It''s still always interesting to know.


What''s even more interesting, and educational, is to see what assembly code the two programs produce. Check it out.... you may learn something interesting about the i386 instruction set, and about optimizing compilers.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
quote:
Original post by nano
If this is all you wanted to say then why the h**l did you post?


I was hinting that the calculation you specified would be applied at compile-time. Does that help untwist your knickers?

Share this post


Link to post
Share on other sites