views:

146

answers:

2

I have a std::set which is of type point

struct point
{
  int x;
  int y;
  int z;
};

Suppose I want to perform three different operation on each variable in set i.e

  • Find the smallest number from x variables.
  • Get missing elements from y variables using set difference.
  • Get product of all z variables.

At this point, should i use three predefined alogrithmic functions in sequence or should I write my own alogrithm which would perform all three operations by iterating over the set the once?

+2  A: 

Start to use 3 functions in sequence. If you get performance issues try to write your own algorithm.

Premature optimization is evil.

onof
Also bear in mind what Knuth wrote just after the quote you paraphrased: "Yet we should not pass up our opportunities in that critical 3%. A good programmer will not be lulled into complacency by such reasoning, he will be wise to look carefully at the critical code; but only after that code has been identified."
Mike Seymour
+8  A: 

Even if you get a ten-fold speed increase, if that piece of code only uses 5% of your application's time, you've just decreased the execution time to 95%. So, unless you know this is a real bottleneck in your application, don't waste time trying to optimize it. And the only way to know this is through profiling.

sbi