iteration over arrary
Which iteration method comsumes more cpu?
Iteration with the [] operator or incrementing a pointer?
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.
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.
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.
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?
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.
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.
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.
quote:Original post by nanoThat''s why the best way is to try it
Compilers do a lot of optimizing and modern cpus do all kinds of crap too
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.
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.
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".
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement