x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 247

How to reduce the "for" loop?

Hi,
I have a question on the "for" loop.
The code is:
for(i=0;i<256;j++){
for(j=0; j<256; j++){
for(k=0; k<256; k++){
temp[i][j][k]=0;
}}}
How can I implement the above code by using other fast
method as the 3 "for" loops are very very slow.
Also, can I use the fast method to extend in 4 or more
"for" loops?
Thanks

0
gorexy
• 4
• 4
• 3
1 Solution

Commented:
0

Commented:
You can use the memset function to set every data member of an array to zero.  In your case, it looks like you can put this:

for (i = 0; i < 256; i++)
{ for (j = 0; j < 256; j++)
{ memset (temp[i][j], 0, 256); }
}

in place of your above code.  That should speed it up some.  Unfortunately, when dealing with large arrays such as the one you have, the functions that come with the compiler may not be sufficient to handle the large numbers.  For example, the optimal situation would be to use the memset function to fill every member of the array in one sweep:

memset (temp, 0, 16777216);

This would fill the entire "temp" array at once, and would no doubt be faster than the method you are currently employing.  However that is not the case.
0

Commented:
Perhaps a redesign of your current requirement for the large array is in order.  Can you please elaborate on your program and what you are doing with this large loop?  Often an alternative method can be found.

In any case, if my above solution (the first one) does not speed it up sufficiently, I think I have a few other ideas, so please feel free to ask.  Thanks!
0

Commented:
memset( temp[i][j], 0, 256*sizeof(temp[0][0][0]) );

(It may also be possible to redesign the algorithm not to require initialization)
0

Author Commented:
Hm...can all the experts send e-mail to me?
Because I seldom check the expert-change web site.
Thanks  my e-mail:00420200@cpccux0.cityu.edu.hk
Alex
0

Commented:
gorexy - Can you simply check the email notification button?  Then you are informed as to when the page is added to (i.e. comments and answers).
0

Author Commented:
OK thresher_shark!
Hm... actually I got a series of number ,for example, 11231236
I would like to treat consecutive 3 numbers as a single unit and count
the number of times that it appears.  For example, 112 -> 1 times
123->2 times, 231-> 1times, 312 -> 1 times, 236 -> 1 times and 361 -> 1 times.
so I am stupid to put them as a 3 dimensional array and count all the combination
So any good suggestions?
Thanks
0

Commented:
You can put them in a hash table.
You can put them in a [10][10]]10] array, which would be much faster to initialize than a [256][256][256] array,
Or you can just put then in a [1000] array
0

Author Commented:
I need to consider all 256 ASCII code and so I need to initialize [256][256][256]!
How I use [1000] array?  The file length is quite long.
0

Commented:
Didn't you say 3 numbers, i.e. digits?

If you need to count all character combinations, you could use a [16777216] array, which could be initialized in a single memset call,
But a hash table may be even better if the array is likely to be sparse.

There's also a trick to defer any initialization until you need it if you have space for two more arrays:

int a[16777216],b[16777216];
int n=0;

if( a[i]>=n || b[a[i]] != i ){
/* temp[i] needs initialization */
a[i] = n;
b[n] = i;
n++;
temp[i] = 0;
}
/* temp[i] has been initiialized */
temp[i]++;

But this may end up being slower over all
0

Author Commented:
Thanks all the experts.  After counting the elements. I need to
do something else.  The code is as follows:

for (i=0; i<256; i++)
for(j=0; j<256; j++)
for(k=0; k<256; k++)
sum+=count[i][j][k]
count=sum-count[i][j][k]
}}}
So any imporvement I can make?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Featured Post

• 4
• 4
• 3
Tackle projects and never again get stuck behind a technical roadblock.