# Unity preventing my code from being optimized away

This topic is 4423 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, Community How can I keep Visual C++ 2005 from optimizing my code away. This happens only on release build. The main reason is for benchmarking, in which the results aren't even close to what they should be.
// Test_Math.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "..\\Math_AsmDouble\\Math_AsmDouble.h"
#include "..\\Math_AsmFloat\\Math_AsmFloat.h"
#include "..\\Math_Intrisic\\Math_Intrisic.h"
#include "..\\System_IntrisicTimer\\System_IntrisicTimer.h"

using namespace std;
using namespace SkepWorks;
using namespace SkepWorks::Math;
using namespace SkepWorks::System;

int _tmain(int argc, _TCHAR* argv[])
{
IntrisicTimer timer;
unsigned long numTimes = 1000000000;

Math::Intrisic::Matrix3 mat(4,8,7,2,5,4,8,7,3);
Math::Intrisic::Matrix3 mat2(5,3,7,6,5,3,4,1,5);
Math::Intrisic::Matrix3 mat3;

timer.Start();

// This loop is optimized away during release builds
for( unsigned long x = 0; x < numTimes; x++)
mat3 = mat + mat2;

float result = timer.GetElapsedTime();
timer.Stop();

cout << result << endl;
cout << 1/result * numTimes << endl << endl;

char ch;

cin >> ch;

return 0;
}


Thanks, exorcist_bob

##### Share on other sites
one technique is to include the output of your calculation in the output of the application itself i.e. print the matrices to the console or print a single value of each matrix so vc++ can't get rid of the computation.

##### Share on other sites
A simple way would be to reference 'mat3' somewhere beneath your loop (in a print statement or something of the such)

There is probably a way to do this in VS, but I don't know the ins and outs of VS very well.

##### Share on other sites
Coming up with useful synthetic benchmarks for code is a bit of a minefield since so many other factors come into play when you use the code in the real world. That said there are some things you can do to get somewhat sensible results out of something like this.

The key thing to realize is that the optimizer is very good at getting rid of redundant work so you need to make sure the work you are doing is not redundant (from the compiler's point of view). For a start you don't do anything with the value you calculate in the loop below so the optimizer trivially recognises the work is redundant and eliminates it. At the very least you need to store the results of your calculations and use them - the easiest way to do that is often to output them to the console or to a file.

The other major problem with your loop is that you perform the same calculation on every cycle through the loop, again the optimizer can see that that is redundant and eliminate the useless repetitions. To address that problem you could accumulate the results of each pass through the loop, thus (hopefully) forcing the compiler to actually carry out every iteration (i.e. change your loop to mat3 = mat3 + mat2, though that wouldn't quite be testing the same thing). Alternatively you could have an output array and two input arrays that are numTimes elements long and operate on a different array element each time through the loop, though again that's not quite testing the same thing since you introduce memory access as a factor (though that's probably a more realistic test anyway).

Even if you follow the advice above you can stil run into unexpected optimizations where the compiler can take advantage of knowledge of the whole program to do optimizations - some compilers might even be smart enough to precalculate the accumulated result of your loop if you used the accumulation I suggest above and simply return the result (I've seen Visual Studio perform similar optimizations). To get around that kind of thing you need to make sure your compiler can't know about your data - read it in from a file or use a random number generator to initialize it.

There are various other tricks you can do to disable optimizations in specific places, like using the volatitle keyword on variables, but you really need to know exactly what you're doing there since you risk disabling legitimate optimziations that the compiler is making and so rendering your test useless.

1. 1
2. 2
3. 3
frob
15
4. 4
5. 5

• 20
• 12
• 13
• 14
• 81
• ### Forum Statistics

• Total Topics
632144
• Total Posts
3004414

×