I need to initialize all the entries in a huge multi-dimensional array (10 * 25 * 200 * 1000) to a value. Is there any efficient way to do it rather than a 4 levels loop? Speed is my main concern. Thanks a lot for your suggestions.
I need to initialize all the entries in a huge multi-dimensional array (10 * 25 * 200 * 1000) to a value. Is there any efficient way to do it rather than a 4 levels loop? Speed is my main concern. Thanks a lot for your suggestions.
Why would you need such an array ? I'm pretty sure you can find a work-around that would do the same job while being neater.Code:int huge_array[10][25][200][1000] = { 0 };
No 'efficient' way to set it to a specific value. But if setting the array to 0, Desolation's technique works.
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
Thanks guys. I need to set all the entries to a value not equal to zero, so I guess I am stuck.
If your array type is int, then memset over the entire thing should be safe:
If it's not an int, then individual assignment will be required, as you described.Code:memset(array, 123, sizeof(array));
If you used the initializer, it would be worse. The compiler would inline all 10*25*200*1000 = 50,000,000 (!!!) calls into your code. A for loop is probably all you have for now.
But Desolation is right; a better solution is probably just waiting to be discovered.
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
I hope you have lots of memory.Code:(10 * 25 * 200 * 1000) * 4 = 200000000
You should re-think your problem.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Have you thought of declaring it as a glolbal variable ?
My knowlege is a bit rusty.. if you declare it as a global variable it should automatically get initialized to 0.
Please correct me if i'm wrong. !!
It doesn't matter if you're right or not:Originally Posted by Machoscorpion
Originally Posted by alois_rone
Quzah.
Hope is the first step on the road to disappointment.