Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Informative "if" statement in "for" loop

Tags:

c

Normally when I have a big for loop I put messages to inform me in which part of the process my program is, for example:

for(i = 0; i < large_n; i++) {
    if( i % (large_n)/1000 == 0) {
       printf("We are at %ld \n", i);
    }
    // Do some other stuff
}

I was wondering if this hurts too much the performance (a priori) and if it is the case if there is a smarter alternative.Thanks in advance.

like image 571
Bunder Avatar asked Jul 02 '13 13:07

Bunder


People also ask

Can you put an if statement in a for loop?

You can put a for loop inside an if statement using a technique called a nested control flow. This is the process of putting a control statement inside of another control statement to execute an action. You can put an if statements inside for loops.

What is an example of an if statement?

if (score >= 90) grade = 'A'; The following example displays Number is positive if the value of number is greater than or equal to 0 . If the value of number is less than 0 , it displays Number is negative .


3 Answers

Maybe you can split the large loop in order to check the condition sometimes only, but I don't know if this will really save time, that depends more on your "other stuff".

int T = ...; // times to check the condition, make sure large_n % T == 0
for(int t = 0; t < T; ++t)
{
  for(int i = large_n/T * t; i < large_n/T * (t+1); ++i)
  {
    // other stuff
  }
  printf("We are at %ld \n", large_n/T * (t+1));
}
like image 50
ChronoTrigger Avatar answered Oct 30 '22 12:10

ChronoTrigger


Regardless of what is in your loop, I wouldn't be leaving statements like printf in unless it's essential to the application/user, nor would I use what are effectively redundant if statements, for the same reason.

Both of these are examples of trace level debugging. They're totally valid and in some cases very useful, but generally not ultimately so in the end application. In this respect, a usual thing to do is to only include them in the build when you actually want to use the information they provide. In this case, you might do something like this:

#define DEBUG

for(i = 0; i < large_n; i++) 
{
    #ifdef DEBUG
        if( i % (large_n)/1000 == 0) 
        {
            printf("We are at %ld \n", i);
        }
    #endif
}

Regarding the performance cost of including these debug outputs all the time, it will totally depend on the system you're running, the efficiency of whatever "printing" statement you're using to output the data, the check/s you're performing and, of course, how often you're trying to perform output.

like image 4
Ed King Avatar answered Oct 30 '22 11:10

Ed King


Your mod test probably doesn't hurt performance but if you want a very quick test and you're prepared for multiples of two then consider a mathematical and test:

if ( ( i & 0xFF ) == 0 ) {
    /* this gets printed every 256 iterations */
    ...
}

or

if ( ( i & 0xFFFF ) == 0 ) {
    /* this gets printed every 65536 iterations */
    ...
}
like image 3
PP. Avatar answered Oct 30 '22 11:10

PP.