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

Single Vs Multidimensional Arrays and the Pic

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Tom Jetland



Joined: 23 Jun 2011
Posts: 31
Location: UK

View user's profile Send private message

Single Vs Multidimensional Arrays and the Pic
PostPosted: Fri Oct 10, 2014 6:17 am     Reply with quote

Hi,

Is there a reason to use a multidimensional array over a single other than just good readability of code? Are they stored differently in the PIC?

Also, I have read that when using a multidimensional array, it is better to increment the second set of elements more frequently than the first, i.e.

Code:

// [ y ][ x ]
int8 myArray[ 30 ][ 100 ];

for ( y = 0 ; y < 30 ; y++ ) {
    for ( x = 0 ; x < 100 ; x++ ) {
        myArray[ y ][ x ] = sumFunc();
    }
}


Thanks for any advice given.


Tom
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Fri Oct 10, 2014 6:58 am     Reply with quote

The array you show in your example will use fully 3/4 of all the SRAM in an 18f4620 , whether it is used as shown or crammed into a single dimension of [3000]

Using dimensions of less than 256 will be faster to resolve than int16's.

BUT
having more than one dimension, and using variables as the element selectors will slow access, compared to a larger single dimension array with the same storage capacity. But again, in your example, its a choice of one int16 or two int8's ........

Depending on compiler optimization, using a defined constant for one of the dimensions in a specific function, "might" be faster than two vars as dimension "pointers" in run time. you would have to check the .LST file to see.

In your specific example - best optimization for your CCS version may well be " 6 of 1 , half dozen of the other".

I'd compile both ways and decide with the .LST file be my guide .
Tom Jetland



Joined: 23 Jun 2011
Posts: 31
Location: UK

View user's profile Send private message

PostPosted: Fri Oct 10, 2014 7:10 am     Reply with quote

Quote:

The array you show in your example will use fully 3/4 of all the SRAM in an 18f4620 , whether it is used as shown or crammed into a single dimension of [3000]


Yeah sorry about that, I just typed some numbers in without thinking :-)

I'll have a compile and see what happens.

Thanks asmboy!


Tom
Ttelmah



Joined: 11 Mar 2010
Posts: 19498

View user's profile Send private message

PostPosted: Fri Oct 10, 2014 8:26 am     Reply with quote

There is always the penalty of multiplication.

If you have (for instance), an array of 20 * 4byte values, then the multiplication to find the location of a value is just *4, which is very efficient (done by shift). If you have a multi dimensional array with 'non binary' indexes (like 30), then you are multiplying by 30 (in the order you show), to 'find' the location in memory. Since the array is over 256bytes, this implies a 16bit multiply (not too bad on a PIC18 or higher). If you can keep the array indexes, or the product of indexes and element size, to a binary value, it saves both code space, and time.
Multi-dimensional arrays, with non binary indexes, made up of of odd sized structures, are 'worst' here....
Tom Jetland



Joined: 23 Jun 2011
Posts: 31
Location: UK

View user's profile Send private message

PostPosted: Fri Oct 10, 2014 10:02 am     Reply with quote

Thanks once again for your input guys.

Tom
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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