CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Indexing through large arrays; more efficient way?
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
RoGuE_StreaK



Joined: 02 Feb 2010
Posts: 73

View user's profile Send private message

PostPosted: Sat May 31, 2014 12:27 am     Reply with quote

Quick revival, I'm about to attempt to bring my old code over to the 24F so thought I'd start with the pointer method to do a speed comparison, before trying the more advanced methods.
Question - if I have say ten arrays, does it hurt to have ten different pointers, or should I use two (need two for two different PWM outputs) and redefine them on the fly? ie., is having 10x pointers no different than just having the 10x arrays, or are there more impacts?

I still have to keep track of the length of the array and where the pointer is up to, correct? ie. it doesn't know how long the array is and will keep incrementing into other data areas unless you stop it?

When the pointer reaches the end of the data (which may not be the exact end of the array), how do I reset the pointer to be back at [0] again for a replay? Is it that n_ptr (for example) is just treated as an integer, and *n_ptr is the array location, so just set n_ptr = 0?
And n_ptr-- will index backwards?

Thanks again for all the input, I'll try to get the basics working again before getting into the uber-optimisations.
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Sat May 31, 2014 2:07 am     Reply with quote

90% of this is standard in C.

There is no immediate difference between using a pointer and an array index, except if the index is a constant. CCS will 'pre-solve' accesses to a constant array index at compile time. So here arrays are more efficient.

Yes, you have to check your pointer against your maximum. The only time the that chips/compilers will do this for you, is on chips that have hardware protection for memory accesses.
Setting the index back to the address of the start of the array moves you back to the start of the array.

A physical array, involves actual memory elements to store the whole array. A pointer is only a memory address and will involve far less storage.

There are though general comments:

The key place where a pointer gains, is when you retain the pointer from loop to loop. If (for instance), you have an array that has elements x bytes long, and instead of using array[n] and array[n+1] as you move forward through the array, you have pointer, and pointer+x, the former is done by multiplying n by x, while the latter only involves a single addition. Even with the hardware multiplier, the addition is the smaller and simpler instruction.
If you have one array of pulse widths for instance, and pre-solve for the last element+1 in the array, then have pointers moving through this array, you just add the size of the element, and when the pointer reaches the element+1 location, set the pointer back to the start of the array, this is more efficient than using array indexes.

Setting a pointer to zero, would point to the first address in memory. Not the start of the array.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group