Rasto
Rasto

Reputation: 17774

Is constructor of class that inherits from another class less effective?

Question

In my application I create large number of instances of 'small' class that just hold some data.

I know that class creation (that is constructor call) is costly. My question is: Will it be more costly if I make this small class inherit from another class ? Some fields will just move to the superclass, so basically I'll can just use

Note: This question is specifically about performance in .NET.


Example situation and demonstration of the question

Consider this 2 cases:

1. case consists of only one class. It is the original class:

public class ShapeWithOffset{
    public double XOffset { get; private set; }
    public double YOffset { get; private set; }
    public IShape Shape{ get; private set; }

    public ShapeWithOffset(double xOffset, double yOffset, IShape shape){
        //check if arguments are correct/throw ArgumentException if not
        XOffset = xOffset;
        YOffset = yOffset;
        Shape = shape;
    }
    //equality members
}

2. case consists of 2 classes where second inherits from first. Second class ShapeWithHorizontalAndVerticalOffset provides the same function as ShapeWithOffset in 1. case.

public class ShapeWithHorizontalOffset{
    public double XOffset { get; private set; }
    public IShape Shape { get; private set; }

    public ShapeWithHorizontalOffset(double xOffset, IShape shape){
        //check if arguments are correct/throw ArgumentException if not
        XOffset = xOffset;
        Shape = shape;
    }
    //equality members
}

public class ShapeWithHorizontalAndVerticalOffset : ShapeWithHorizontalOffset{
    public double YOffset { get; private set; }

    public ShapeWithHorizontalAndVerticalOffset(double xOffset, double yOffset, 
                                                IShape shape) : base(xOffset, shape){
        //check if yOffset is correct/throw ArgumentOutOfRangeException if not
        Yffset = yOffset;
    }
    //equality members
}

My question is: Is command var foo = new ShapeWithOffset(20, 10, shape); faster then
var boo = new ShapeWithHorizontalAndVerticalOffset(20, 10, shape); ?

If I used composition instead of inheritance it would be... But what about with inheritance?

Upvotes: 2

Views: 327

Answers (5)

MoienGK
MoienGK

Reputation: 4654

talking generally about inheritance (not specifically this your case) as kuchana said in his book (design patterns) try to avoid inheritance if not necessary, because of its complexity. and i think runtime binding may cause in small performance issues, but the main overhead of inheritance is its complexity in design, that is much more important than a tiny performance issue .

EDIT

i now realize that question is about constructing :D. the following paragraph is true in java , maybe it is true in .Net too. i think, if you explicitly define constructor in your child class, it wont make any difference , because the compiler will look for a constructor in the child class first, if not found it will go up in inheritance hierarchy, till he! find the proper constructor.

Upvotes: 0

Dan Bryant
Dan Bryant

Reputation: 27495

My experience with micro-optimizing code using a profiler is that method call overhead is negligible. My main wins for performance improvements have almost always been: caching frequently used calculations, optimizing deep inner loops (usually by using direct array indexing rather than enumeration) and avoiding unnecessary boxing. All of these are dwarfed, of course, by improving the algorithm to reduce the number of iterations (usually by introducing a heuristic than can exclude branches early or by transforming the problem into separate 'aggregate' and 'detail' phases.)

Upvotes: 3

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262919

First, as I said in my comment, calling base constructors (and virtual methods) does not affect performance enough to justify losing the maintainability and expressiveness it confers to your program. If performance really was a problem in that case, structured languages would not exist.

Now, from a design point of view, the question is reduced to whether or not inheritance is actually profitable in your case. Which prompts a question: why can't ShapeWithOffsetderive from Shape? If it was possible, I'd probably cut off the complexity and do something like:

public class ShapeWithOffset : Shape
{
    public ShapeWithOffset(double xOffset)
    : this(xOffset, 0.0) {}

    public ShapeWithOffset(double xOffset, double yOffset)
    {
        // TODO - Check if arguments are correct/throw ArgumentException if not.
        XOffset = xOffset;
        YOffset = yOffset;
    }
}

In C# 4, you can even write:

public ShapeWithOffset(double xOffset, double yOffset = 0.0)
{
    // TODO - Check if arguments are correct/throw ArgumentException if not.
    XOffset = xOffset;
    YOffset = yOffset;
}

Upvotes: 1

Daniel Renshaw
Daniel Renshaw

Reputation: 34177

This smells like premature optimization

The key words in your question are "... I think ...". If you know you have a performance problem then identify the bottleneck and optimize that. If you are anticipating a performance problem, don't! It is exceedingly unlikely that any performance bottleneck will be caused by using inheritance in the way your suggest. But, if you really want to know in advance, test in advance.

Upvotes: 8

Jakub Míšek
Jakub Míšek

Reputation: 1050

This will be almost the same. Calling of the base .ctor will be inlined by JITter and the allocation is performed once only. It will cost you only a few bytes as an overhead.

If you are not sure, try a small benchmark.

Upvotes: 5

Related Questions