A while back I read about a way for either gcc, or gnu, or something to help optimize a program. One had to add a certain #define and then maybe also a compiler flag. It might behave like this:
1 2 3 4 5 6 7 8 9 10
#include <vector>
int main( void )
{
std::vector< int > numbers;
for ( int i = 0; i < 100; ++i )
numbers.insert( numbers.begin(), i );
return 0;
}
$ ./a.out
Using std::vector push_back would be much better
Using std::list would also be better
It's not those. It's like a profiler, but when you run the program it considers alternative methods/containers that would help the program. It also displays a percentage gain over the current setup.
gcc usually goes hand to hand with gdb and gprof which are debugger and profiler respectivelly. I did not heard about anything similar and even static code analysers are not that good.
Also in your code inset cannot be replaced by push_back because it would change code semantics.
They are not changing anything in compilator behavior. It is defined be compiler depending on flags so your preprocessing code can detect them and maybe change code depending on that.
In a couple of tutorials for OpenGL I have seen something similar, so I know what you are talking about. I don't know for GCC, but for VS at least some examples are using macros for the <math.h> functions rather than functions, and removing bounds checking on the STL [] operators.
#include <vector>
int main( void )
{
std::vector< int > numbers;
for ( int i = 0; i < 100; ++i )
numbers.insert( numbers.begin(), i );
return 0;
}
You should just reverse your loop and add to the end. Or use deque.
Also useless advice. The correct way to fully optimize this program would by to modify it to the following:
1 2 3 4
int main( void )
{
return 0;
}
Jokes aside, if it can tell you how something can be improved that you didn't notice then it's not useless; even if you find a better solution yourself after it brings it to your attention.
@iHutch
Yes, that should be correct. However, for some reason, MSVS gives some sort of bounds checking for the [] operators in Debug mode, as can be seen here (2010 Vector):