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;
}

>>I want to optimize the following small integer program in speed

May I ask why?

>>I want to optimize the following small integer program in speed

May I ask why?

This code snippet is part of a larger program which will run for a long time.

Have you profiled the running program and confirmed that there is a bottleneck? Why optimize if the program is fast enough already? :)

-O3 goes without saying if you need to program to be fast.
How a function object is supposed to speed up anything is beyond me, though.

I don't see what can you save by squeezing out anything from that tiny bit of code shown.

Take a look at this: Optimization: your worst enemy, which might clear up a few things for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.