15

OK, here's what I want :

  • I have written several REALLY demanding functions (mostly operating on bitmaps, etc) which have to be as fast as possible
  • Now, let's also mention that these functions may also be grouped by type, or even by the type of variable on which they operate.
  • And the thing is, apart from the very implementation of the algorithms, what I should do - from a technical point of view - in order not to mess up the speed.

And now, I'm considering the following scenarios :

  • Create them as simple functions and just pass the necessary parameters as arguments
  • Create a class (for 'grouping'/organisation purposes) and just declare them as static
  • Create class by type, e.g. Create a class for working on bitmaps, create a new instance of that Class for every bitmap (e.g. Bitmap* myBitmap = newBitmap(1010);, and operate on it with its inner methods (e.g. myBitmap->getFirstBitSet())

Now, which of these approaches is the fastest? Is there really any difference between straight simple functions and Class-encapsulated static functions, performance-wise? Any other scenario that would be preferable, which I haven't mentioned?


Sidenote : I'm using the clang++ compiler, for Mac OS X 10.6.8. (if that makes any difference)

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
  • 1
    If you are really concerned about the performance, You should profile both the versions you propose on your envrionment.That way you get a good diagnostic rather than meere guesses which the answers here will provide you. – Alok Save Dec 14 '12 at 06:46
  • @LuchianGrigore+AlokSave To be honest, nope, I haven't run any profiling test. Yep, I know some things are better to be diagnosed based on the exact environment they are supposed to be running on; I guess I needed some opinion on some bits of the theory behind my dilemma, I might be missing. *(e.g. even though I have considered it, could a class creation and method call be faster than a simple function call? Still guessing... though I think I'm guessing right...)* – Dr.Kameleon Dec 14 '12 at 06:52
  • Without profiling, only major performance cost comes if your members are virtual functions(or is it if your class has virtual functions) – Karthik T Dec 14 '12 at 06:52
  • 1
    I'd code for readability and abstract the call away, that way when you do profile it's easy to change and test alternatives. – Luchian Grigore Dec 14 '12 at 06:53

2 Answers2

25

At CPU level, there is only one kind of function, and it very much ressemble the C kind. You could craft your own, but...

As it turns out, C++ being built with efficiency in mind maps most functions directly to call instructions:

  • a namespace level function is like a regular C function
  • a static method is like a namespace level function (from a call point of view)
  • a non-static method is very similar to a static method, except an implicit this parameter is passed on top of the other parameters (one pointer)

All those 3 have the exact same kind of performance.

On the other hand, virtual methods have a slight overhead. There was a C++ technical report on performance which estimated the overhead compared to a non-virtual method between 10% and 15% (from memory) for empty functions. Meaning that for any function with meat inside (ie, doing real work), the overhead itself is close to getting lost in the noise. The real cost comes from the inhibition of inlining unless the virtual call can be deduced at compile-time.

Community
  • 1
  • 1
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
4

There is absolutely no difference between classic old C functions and static methods of classes. The difference is only aesthetic. If you have multiple C functions that have certain relation between them, you can:

  • group them into a class;
  • place them into a namespace;

The difference will again be aesthetic. Most likely this will improve readability.

In case if these C functions share some static data, it would make sense (if possible) to define this data as private static data members of a class. In this case variant with the class would be preferable over the variant with namespace.

I would discourage you from creating a dummy instance. This will be misleading to the reader of the source code.

Creating an instance for every bitmap is possible and can even be favorable. Especially if you call methods on this instance several times in a typical scenario.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51