Is there more efficient way to fill whole the array with the same value?
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
array[i][j] = 0;
Is there more efficient way to fill whole the array with the same value?
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
array[i][j] = 0;
The memset() function will do that. Internally, it still needs to iterate over every array element, though, so it is not necessarily more efficient (except that it treats the array as a one dimensional array, so there are fewer array index calculations performed).
what is the datatype of array[] ?
Is there more efficient way to fill whole the array with the same value?
Logarithmic fill is probably the most efficient, if you have a block move instruction.
Logarithmic fill
que?
Put fill value into first element.
Copy first element into second.
Copy first and second elements into third and fourth elements.
Copy elements zero to three into four to seven.
Copy elements zero to seven into eight to fifteen.
And so on.
Mop up any leftovers.
Worked great on the transputer. (like I said, needs a block move instruction)
I see, just
rep movsd;//do it!
Maybe not starting from 1 cell and starting from say filling 4 cells to minimize time. If you start from 1 cell and copy to the next, then copy 2 to 4 cells, you may save some time just loop-fill 4 cells, depending on how fast the block move runs compared with single cell. Auto address register increment should save time once you move enough stuff at a time cause there's overhead for a block move such as setting up registers to contain source and dest address and counter.
Although it only works for zero, you can clear an array of any int/long/char type easily with memset():
memset(array,0,sizeof(array));
The sizeof(array) returns the size of your array in bytes - since it automatically recalculates when compiled, you never have to change it when you change your array size. And because it is a compiler directive (although it looks like an ordinary function), the result compiles to a constant value in your code.
The result is about the fastest way to clear an array.
Of course, it only works with arrays that make sense of all zeroes - char, int, long - I wouldn't recommend it for a float array since all zeroes may be interpreted different ways in different compilers, and you've then created a potential bug waiting to happen. And of course never use it for objects...
I wouldn't recommend it for a float array since all zeroes may be interpreted different ways in different compilers
Arduino uses IEEE 754 definition for floats. From the IEEE 754 float definition:
0000 0000 = 0,
8000 0000 = -0
so using 0 should work!
Further on setting up arrays - I got caught up in checking whether memcpy() could be used for a fast fill. The resulting explanation and code was too long to fit here, but if anyone is interested, I've posted it online:
http://www.utopiamechanicus.com/149/arduino-setup-arrays/
However, the end result is you could fill an array of almost anything with almost anything close to the speed of memset() like this:
int x[200];
FILLARRAY(x,1345);
Does that require some library to be included for a Arduino sketch?
retrolefty:
Does that require some library to be included for a Arduino sketch?
No - it's just a macro to wrap around memcpy();
#define FILLARRAY(a,n) a[0]=n, memcpy( ((char*)a)+sizeof(a[0]), a, sizeof(a)-sizeof(a[0]) );
The article just goes on to explain the pros and cons, and provide other options, such as a function call.
The article just goes on to explain the pros and cons, and provide other options, such as a function call.
Thanks for that. Macros are something I try to avoid if possible as I'm still in a learn the basics of C/C++
Macros are something I try to avoid if possible
You are a wise man, retrolefty.