Unity preventing my code from being optimized away

This topic is 4339 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.

• 9
• 13
• 40
• 15
• 11
• Similar Content

• Ok, firstly, Hi.

This is my first post on this forum. I am an Indie Dev making my first game so bear with me when I say dumb stuff, I'm on a huge learning curve.

My first question is about inventory systems for unity. I am trying to make a survival type game with crafting. I have purchased Inventory manager pro by devdog from the unity asset store and it seems like a pretty powerful assett but for an intermediate coder its a little tough to use.  I'm beginning to wonder if it was the right purchase.
So my question is.... does anyone have any experience of inventory plugins / systems for unity and can anyone reccomend a system to me?
It needs to have the following: Loot system, crafting system, character sheet, blueprint system,  character stats system. Ideally with as little coding as possible.

Thanks

• I've got a bug with my brick breaker style game. The bricks move down one line at a time ever 1.5 seconds. What appears to be happening is occasionally the ball will be just about to hit the brick when the brick moves down a line, and now the ball is behind it. I'm not sure how to fix this. I have two ideas but I'm not sure of implementation. 1 solution would be to check where they were and where they are going to be before rendering the frame. Then if they crossed paths, then register the brick as hit. Solution 2 would be change how the bricks move. I could maybe slide them down line by line, instead of a jump down. I'm not sure of this will fix the issue or not. Any ideas?
• By Pixeye
I wrote an extension for unity inspector that allows to group/fold variables.
Available on github  , cheers!

• I'M interested in programming tools (For animation, UI, etc). Can anyone suggest me the resources where I can start learning or which technologies I need achive it.

Thanks,
Rakshit
• By Paszq
Group photo of some of the characters and creatures currently living in Arpago