Jump to content
  • Advertisement

Archived

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

nano

iteration over arrary

This topic is 5460 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!