3 Optimizations for speeding Visual C++ compiled code.

For fun, I participated in a programming contest. Instead of describing the process this time, I thought I'd include a function. Can you guess what it does? I'm changing some of the variable names so it isn't obvious.

  1.  
  2. unsigned short result [dim][dim];
  3. void foo(const string& info1, const string& info2, string& ret_val) {
  4.  
  5. for (int x=0;x<dim;++x) {
  6. for (int y=0;y<dim;++y) {
  7. result[x][y] = (info1.at(x)-'0') * (info2.at(y)-'0');
  8. }
  9. }
  10.  
  11. ret_val.clear();
  12.  
  13.  
  14. // and now, the evil
  15. unsigned short carry=0;
  16. unsigned short sum=0;
  17. int xPos,yPos;
  18. bool use_high;
  19. for (int x=dim*2-1;x>=0;--x) {
  20.  
  21. use_high=x<dim;
  22. xPos= x<dim ? 0 : x-dim;
  23. yPos= x<dim ? x : dim-1;
  24. sum=0;
  25. do {
  26. sum +=
  27. use_high ?
  28. result[xPos][yPos] / 10 :
  29. result[xPos][yPos] % 10 ;
  30. if (use_high) {
  31. --yPos;
  32. } else {
  33. ++xPos;
  34. }
  35. use_high=!use_high;
  36. } while ( xPos<dim&&yPos>=0);
  37. sum += carry;
  38. carry = sum / 10;
  39. ret_val.insert( ret_val.begin(), (char)(sum % 10 + '0') );
  40. }
  41. }
  42.  

Anyhow, now that that fun is over, I was reading comments on a C++ forum and someone mentioned a few tips to optimizing C++ code with Visual C++. I thought I'd do a quick comparison between these and see how much of a difference it made since I was interested in program execution time and already had timing code built into my program.

  1. Release build

    I was already doing this, but I thought I'd include it for the sake of being complete.

    Debug build running time: 0.09 seconds
    Release Build running time: .02 seconds

    Obviously, this is a big one.

  2. Optimizations

    Optimizations are usually the biggest difference between release and debug builds. By default, my compiler was adding /O2 to build for maximum speed. I decided I'd enable every other optimization possible

    Release build optimizations settings

    Release build optimizations settings

    Anyway, the funny thing was, it ran faster by leaving the settings as they were. Obviously, you'll need to test these and know what they do before simply enabling them. One thing that did seem to make a difference though, was to link against the Multi-threaded library instead of the Multi-threaded DLL. (/MT instead of /MD)

  3. Disable secure stl

    I found references to adding /D_SECURE_SCL=0 to the compiler command line. I assume if you heavily use iterators with STL, this might make more of a difference. For my little program, it didn't seem to do much at all. I'm not completely sure it was even working. I'm assuming for now though, that it will do something, but that my program isn't a good test case.

2 Responses to “3 Optimizations for speeding Visual C++ compiled code.”


  1. Peter

    Big integer multiplication?

  2. Dennis

    You nailed it! I think the sum/carry variables are a giveaway actually. Perhaps I should have renamed those too.



css.php