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;
7 Years
Discussion Span
Last Post by Rajesh R Subram

>>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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.