3

Let's say I have an object A and B that extends A. B has global variables that are irrelevant for A (an array, and a few counters).

Since explicit casting is costly (I'm not sure how much), would it be better, from a sheer performance pov, to only create a class A and create an array only if needed so that I don't have to cast?

I guess the question is, do global variables of an object cost anything at all, even if unused?

Edit: forgot to add the most important... functions, obviously.

zuokuok
  • 323
  • 1
  • 6
  • 16

3 Answers3

2

In recent years, inheritance is often treated like code-smell, because it can lead to different problems: https://dzone.com/articles/is-inheritance-dead

If we talk in pure performance term, an empty array takes about 8 bytes in RAM (4 bytes store length and 4 bytes a reference, but it is a little platform-dependent: How much space does an array occupy?). So, even if you have a thousand of such objects, one array field will take approximately 1_000 * 8 bytes ~ 8 KBytes in RAM.

As you probably know, nowadays phones usually contain > 1 GByte of RAM. However, don't forget that your app usually can take from 60 to 192 MBytes of RAM (Detect application heap size in Android).

In any case, it is more than enough not to count every little field that you are adding to your class.

However, going back to my first statement, I suggest you to think about solving the problem using composition instead of inheritance, as it is suggested in Effective Java

Update
About performance, I would suggest you to read this topic: The performance impact of using instanceof in Java Are you sure that you need such type of premature optimization? Or is it more a theoretical question than practical?

Community
  • 1
  • 1
Gaket
  • 6,533
  • 2
  • 37
  • 67
  • To answer the question, using one class is then better performance-wise? – zuokuok Dec 10 '16 at 22:06
  • It depends on: 1) number of objects that you will have 2) your use case: how will you use this objects. Can you update your question with this additional info? – Gaket Dec 11 '16 at 07:25
1

Obviously, No, don't blend both classes in one class A. Never. as you mentioned, the array is irrelevant to class A. so don't put it to A.

next, in your case, downcasting is a point that tells: Wait programmer, do you want to think a little more? sometimes, there is a solution that does not need downcasting. but

Since explicit casting is costly

I don't think so. There is some benchmark and expansions that tells us, no there is no huge difference here.

expansion

benchmark

So. follow the first solution...

Community
  • 1
  • 1
Mehdi Khademloo
  • 2,754
  • 2
  • 20
  • 40
0

Since explicit casting is costly (I'm not sure how much), would it be better, from a sheer performance pov, to only create a class A and create an array only if needed so that I don't have to cast?

Besides the dubious claim about the cost of casting, maybe I'm misunderstanding but that raised red flags for me. If your design involves casting an object to a subclass it could probably be better designed in some other way. I can't say what without seeing more, but the need to cast is often a sign of design failure*. As Gaket suggests, composition could be better, or there might be some other change - factory pattern perhaps?

Secondly, you should only be concerned about performance if you have noticed a performance issue, or have a real reason to think there will be one. Almost anything the processor does is going to be plenty fast unless it's done many times over (like millions). Your performance issues (unless your program is unusual in some way) are most likely going to be I/O or memory. Then after you're identified a performance bottleneck, the steps to fix it are:

  1. measure
  2. set a goal
  3. fix
  4. measure

Don't leave out any steps.

*I know casting is a central part of Android programming because you have to cast Views every time you get them, and I wonder if there might have been a better way to do that, but that's what we're stuck with

nasch
  • 5,330
  • 6
  • 31
  • 52