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,
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With