Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to reduce the "for" loop?

Posted on 1998-10-15
11
Medium Priority
?
238 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 300 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 85

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 85

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 85

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

578 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