3

I develop game with openGL 2D on Android.

It's very impotent to gain performance level since my view is complicated and contains 300-400 objects at the same time (game loop).

I know pretty good about static and non-static fields in java:

private final static int NUMBER = 10;

and

private final int NUMBER = 10;

My question is not encapsulation (as OO) but performance.

What is better to use for performance, static or non-static fields.

The idea is that for most my logic I use primitives like float/int.

I create generic super class for all my "Views" and this class must be more efficient in scope of performance:

Here is example:

    /** Sprite sheet definition */
private final int SPRITE_WIDTH = 4;
private final int SPRITE_HEIGHT = 4;

private float mScreenWidth, mScreenHeight, wRatio, hRatio;
private int mFrame = 0;
private int mSwitcher = 0;
private final int TEXTURE_COUNT = 1; // for sprite sheet we use 1 image all the time.
private int[] textures = new int[TEXTURE_COUNT]; // frame animation

protected FloatBuffer vertexBuffer;

private final ESpriteDirection mDirection = ESpriteDirection.TOP_TO_DOWN_LEFT_TO_RIGHT;


public float x, y, initPos, finalPos, initSpeed, currentPos;

private ByteBuffer bb1;

private final int TOTAL_IMAGE_COUNT_IN_SPRITE = SPRITE_WIDTH * SPRITE_HEIGHT;

private FloatBuffer[] floatBufferArray = new FloatBuffer[TOTAL_IMAGE_COUNT_IN_SPRITE];

private float xOffset = 1.0f/SPRITE_WIDTH;
private float yOffset = 1.0f/SPRITE_HEIGHT;




private float vertices[] = {            
        0.0f,3.0f,0.0f,
        0.0f,0.0f,0.0f,                 
        3.0f,3.0f,0.0f,
        3.0f,0.0f,0.0f  
};


private float storage[][] = new float[TOTAL_IMAGE_COUNT_IN_SPRITE][];
private int[] sprite_X_Indexes = new int[SPRITE_WIDTH];//{1,2,3,4}; 
private int[] sprite_Y_Indexes = new int[SPRITE_WIDTH];//{1,2,3,4};

I can guess that for multiple initiation of the same class static field is better, hmm

Thank you,

7
  • 6
    I'd advise you to avoid recurring to that optimization that is going to make your design worse unless you profiled your application and found out that the issue relies in the access to that fields. Commented Jul 19, 2013 at 12:49
  • 4
    static has nothing to do with performance. If the field belongs to an instance, it must not be static. If it belongs to a class, it must be static. Commented Jul 19, 2013 at 12:49
  • 2
    Do you mean static/non-static or final/non-final? In your example, both fields are static. Commented Jul 19, 2013 at 12:49
  • 2
    Both variables in your example are static. Commented Jul 19, 2013 at 12:49
  • @adenoyelle, sorry, fixed Commented Jul 19, 2013 at 12:50

1 Answer 1

9

From a performance POV, once your code has been compiled there should be pretty little difference, since both your static and a (single) non-static field have been transformed into just a memory location.

Declaring the field static may provide some optimization possibilities at the compiler level. On the other hand, typically static objects are likely not "close to" your instance data in memory, which is bad for your cache performance.

In general, this is not where you should spend your time until / unless you have exhausted all algorithmic optimizations, and then only after you know this is actually where you lose time.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.