I want to optimize the following small integer program in speed.
My g++ compiler version is 4.3.4 on 32 bit linux.
Please suggest or comment the following ideas.
Some ideas are:
1. use compile flag such as -O3
2. rewrite the bigger function with function object
#include <algorithm>
#include <cstdlib> // for abs()
using namespace std;
const int N = 50;
const int D = 20;
bool bigger (int i) { return abs(i) >= D; }
int main()
{
int seq[N], diff[N];
// initialize seq
// ...
adjacent_difference(seq, seq + N, diff);
int count = count_if(diff + 1, diff + N, bigger);
// ...
return 0;
}
If the body of operator() within the function object can be seen by the compiler at
the point of invocation of count_if (above), then the compiler can inline the function.
With the function pointer case, even if the body of the function is seen by the compiler
at the point of invocation of count_if, it still cannot be inlined.