Solved

How to reduce the "for" loop?

Posted on 1998-10-15
11
214 Views
Last Modified: 2010-04-15
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
Comment
Question by:gorexy
  • 4
  • 4
  • 3
11 Comments
 
LVL 6

Accepted Solution

by:
thresher_shark earned 100 total points
ID: 1253575
Answer coming....
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1253576
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
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1253577
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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 84

Expert Comment

by:ozo
ID: 1253578
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 Comment

by:gorexy
ID: 1253579
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
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1253580
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 Comment

by:gorexy
ID: 1253581
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
 
LVL 84

Expert Comment

by:ozo
ID: 1253582
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 Comment

by:gorexy
ID: 1253583
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
 
LVL 84

Expert Comment

by:ozo
ID: 1253584
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 Comment

by:gorexy
ID: 1253585
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

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to connect an iphone application to mysql database 10 207
posix semaphore deadlock 13 123
Need example 5 118
sameEnds challenge 3 167
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question