Syndicate content

C++ automatic optimizations

Once in a while I check how the compilers are behaving in such and such situations to make sure that when I wrote code it gets properly optimized. Today I was surprised as I tried to put a break point of a variable and it looked like the compiler wasn't using it. Indeed, the optimizer 100% removed the variable from the final code. Quite interesting since trying to reverse engineer this assembly language would probably end up using a goto statement... (ouch!)

The code goes more or less like this:

  found = false;
  for(idx = 0; idx < max; ++idx) {
    ... do something ...
    if(condition == true) {
      found = true;
      break;
    }
    ... do something else ...
  }
  if(!found) {
    ...
  }

This is the only place where the variable found is used. (This is an important point because some optimization won't be possible if you reuse the same variable over and over again in the same function.)

As you can notice, if the loop ends without finding anything, then the if() block after the loop is executed. To the contrary, if the condition inside the loop is true and the found variable is set to true, then the if() block can be skipped. In other words, the break statement can actually be transformed into a goto AFTER the if() block. This means the variable is not required. Rewritten with a goto, the code would look like this:

  for(idx = 0; idx < max; ++idx) {
    ... do something ...
    if(condition == true) {
      goto got_it;
    }
    ... do something else ...
  }
  // when not found
  ...
got_it:

And as you can see, the need for the found variable can easily be eliminated. Note that when the if() block is true, it can simply be moved right before the break state and similarly, you do not in anyway need the found variable!

Syndicate content

Diverse Realty

Diverse Realty Team

Want a New Home?
Want to Sell Your House?

Call Alex at
+1 (916)
220 6482

Alexis Wilke, Realtor
Salesperson
Lic. # 02024063

Cory Marcus, Broker
Lic. # 01079165

     

Terms of Site Index

Find the page/content you are looking for with our index.

  • IsObject
  • freeware
  • missing
  • recordset
  • vsftpd

    Very Secure File Transfer Protocol Daemon, an FTP server that allows people to transfer data between computers. It is marked as very secure, at least it is as secure as an FTP server can be (although there has been several security advisories for this tool...)

    Remember that an FTP transfer is rarely using an encrypted pipe (it is possible to use kerberos and SSL to really secure the data transmission) and also because it uses a data stream opened dynamically, it is possible for hackers to spoof a data connection.