Which is more efficient memory wise: static functions, or functions of an object that is deleted right away?

The question as what the title says, static functions? or functions of an object that is deleted right away?

I know that in a real situation the difference is completely unnoticable but i would still like to know which is more efficient in saving memory. I really don't mind the overhead given by the "new" and "delete" command.



myObject = new MyClass;
delete myObject;

edit: the second code might as well be MyClass().normalFunction(); silly...

there are a few things to consider here;

  1. there will only be one instance of the myObject, and it is only used ONCE in the application.
  2. after usage, it is deleted right away because it is not needed.
  3. one would ask, why is this even in a class? why not just put the function where it is used with temporary variables? the answer is encapsulation and readability. i do believe static functions use the same resources as global functions since in fact, they really are global functions that enjoy class scope. the only reason i have to put it in it's own class is to make my code more readable, and encapsulation.


As it stands, none of this makes any sense. The real solution would be to provide a free function (a function at namespace scope), because this is what free functions are there for.

Oh, and since you asked: If calling this one single function has noticeable overhead in your code, then you will only find out about it through careful profiling. Profiling is also what would answer your question which way is faster. But first make sure your code is easy to read and well maintainable. Optimizing this then will be much easier than fixing prematurely micro-"optimized" code. The only early optimizations you should employ are those that result in optimal data structures and algorithms.

(Note that new and delete will very likely have far greater overhead than what the function actually does, let alone calling it.)

in fact, they really are global functions that enjoy class scope

I think that is spot on.

It doesn't make much of a difference. However, the following would make a lot more sense:

    MyClass myObject;

Or even,


Why would you bother creating a heap-allocated instance of an object that doesn't even matter?

We can't say for certain without trying it on a specific platform (or knowing the details of that platform), but we can probably say that the static one will not be slower than the one with new & delete.

There is no overhead to calling non-virtual member function versus class static function versus a free function since binding is resolved at compile-time. The only difference is that member functions get one extra argument for this pointer, but with static and free function you have to pass the object somehow, so it's the same.

If the code is complex enough that you think it needs to be in its own class, then that suggests multiple methods and state stored in the object. You're asking to compare that to using multiple functions, and by implication state passed as arguments. If there's a lot of shared state between the methods, using an object is reasonable. If there's not, using multiple functions is reasonable.

For scoping you may just prefer using namespace.

On the other hand, since you're asking for memory efficience, I can see one reason why you may prefer the object. If you're going to have a consider amount of memory allocated, encapsulating it in the object members sounds like a way to free them afterwards.

Need Your Help

Function returns string on first call, and an object on subsequent calls

php mysql orm propel

I'm a little bit baffled by this, so I'm hoping someone can shed some light on it for me.

How to delete every character after a space?

linux bash unix scripting

I am making a script that creates a backup of my /home/ directory called backup.sh. When the backup completes I want the script to spit out the size of the backup in megabytes. Here are the lines I...