Class methods VS Static functions VS VS Simple functions - in terms of performance?

OK, here is what I want:

  • I wrote some REALLY requiring functions (mainly working on bitmap images, etc.) that should be as fast as possible
  • Now let's also mention that these functions can also be grouped by type or even by the type of variable in which they work.
  • And the fact is that, in addition to the implementation of the algorithms, I have to do - from a technical point of view - so as not to spoil the speed.

And now, I am considering the following scenarios :

  • Create them as simple functions and just pass the necessary parameters as arguments
  • Create a class (for grouping / organization purposes) and just declare them as static
  • Create a class by type, for example. Create a class for working with bitmaps, create a new instance of this class for each bitmap (for example, Bitmap* myBitmap = newBitmap(1010); and operate on your own methods (for example, myBitmap->getFirstBitSet() )

Now, which of these approaches is the fastest? Is there any difference between direct simple functions and functions of a static class class in terms of performance? Any other scenario that would be preferable that I haven't mentioned?


Sidenote: I am using the clang++ for Mac OS X 10.6.8. (if that matters)

+7
source share
2 answers

At the CPU level, there is only one kind of function , and it very much resembles the C-type. You can create your own, but ...

As it turned out, C ++, built with efficiency in mind, displays most functions directly for invocation commands:

  • The namespace level function is similar to the regular C function
  • the static method is similar to a namespace level function (in terms of invocation)
  • the non-static method is very similar to the static method, except that the implicit this parameter is passed on top of other parameters (one pointer)

All three of these have the same level of performance.

virtual methods, on the other hand, have little overhead. A C ++ technical performance report was presented that estimated overhead compared to the non-virtual method between 10% and 15% (from memory) for empty functions. This means that for any function with meat inside (i.e., performing real work), the overhead by itself is close to delusion in noise. The real cost comes from braking the insert if the virtual call cannot be output at compile time.

+14
source

There is absolutely no difference between the classic old C functions and the static class methods. The difference is only aesthetic. If you have several C functions that have a specific relationship between them, you can:

  • group them into a class;
  • put them in the namespace;

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

I would discourage you from creating a dummy instance. This will mislead the source code reader.

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

+3
source

All Articles