Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  double[] or float[] vs JVector3 class  (Read 3086 times)
0 Members and 1 Guest are viewing this topic.
Offline PeterB

Junior Devvie





« Posted 2005-10-23 18:40:10 »

Do people prefer to represent 3D points in space using arrays of double / float primitives, or a custom class?
I use a class similar to the one below, but I'm wondering if there's a speed benefit (even if it's a small one) to using an array of doubles instead?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public class JVector3
{
    double x = 0;
    double y = 0;
    double z = 0;
   
    JVector3 ()
    {
        this.x = 0.0;
        this.y = 0.0;
        this.z = 0.0;
    }
   
    public JVector3 (JVector3 v)
    {
        this.x = v.x;
        this.y = v.y;
        this.z = v.z;
    }
}


The only single immediate advantage I can see to using double[] instead of JVector3 is the ability to quickly iterate through all the points in the array:
1  
2  
    for (int i=0; i < darray.length; i++)
        darray[i] *= 6;


Thoughts?

Vault101 / Mace The Game
There are 10 kinds of people in the world. Those who understand binary and those who don't.
Offline Vorax

Senior Devvie


Projects: 1


System shutting down in 5..4..3...


« Reply #1 - Posted 2005-10-23 19:19:03 »

EDITE: I kinda missed the point of the question; I do float only...but see below

My vector class does both primatives and arrays - and also can do a NIO buffer representation.  I store the base as floats, but have an uninitiazlied array and FloatBuffer pointer.  If the methods are called I lazy instantiate the array or NIO buffer (only once with an if (array == null) ), then populate it and return the array or buffer.  This saves on memory as well because you only instantiate what you actually need.  For further optimization, I have the option of setting a flag that says the data is constant, therefore the buffer or array population only happens once if called and then never again unless the flag is flipped.

Online princec

« JGO Spiffy Duke »


Medals: 434
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #2 - Posted 2005-10-23 19:53:07 »

Arrays all the way for me, where the number of elements is suitably large. Object overhead of having 100k vertices would amount to 1.2MB. Which also unnecessarily taxes the garbage collector.

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2005-10-23 19:57:34 »

If you're running Client VM and you care about performance, don't use a big float[]. It will be slower than fields.

If you care about quick and clean coding, don't use float[]s as it will be a nightmare to debug.

Float fields are normally more than accurate enough for all math operations, and take less ram than doubles. Put as much data structures in tiny objects as you want, it will really help you build your project quickly and hassleless. Not much can outperform field-access and array-access is definitly slower.

But... all rules change when you start die-hard game-programming Grin

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline PeterB

Junior Devvie





« Reply #4 - Posted 2005-10-23 20:13:24 »

Thanks for the replies!
Is this really true? :

Quote
Not much can outperform field-access and array-access is definitly slower.

I thought array access was very fast if referencing it using an index e.g. myvalues[2]
Take the following:

 byte[] b = new byte[3];
 // sets aside an area of memory 3 x  8 bits in size (plus any additional JVM stuff)

byte[0]  // goes to memory location start, reads off 8 bits
byte[1] // goes to memory location start + (8*1), reads off 8 bits
byte[2] // goes to memory location start + (8*2), reads off 8 bits

I'm probably thinking too much about this, but I've been using Java for over 5 years and come from a C++ background where it was really handy to know all this.

Oh, and I'm using doubles because I couldn't believe the result of the following code was 0.049999997 !
1  
2  
3  
4  
5  
6  
7  
   float f = 0.0f;
   f += 0.01f;
   f += 0.01f;
   f += 0.01f;
   f += 0.01f;
   f += 0.01f;
   System.out.println (f);

Vault101 / Mace The Game
There are 10 kinds of people in the world. Those who understand binary and those who don't.
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2005-10-23 20:19:59 »

Client VM:

byte[] b = new byte[3];
byte a = b[0]; // VM: is "0" equal or more than 0, is "0" less than 3 ? if so, read from memory and store into a, else: throw java.lang.ArrayIndexOutOfBoundsException
byte b = b[1]; // VM: is "1" equal or more than 0, is "1" less than 3 ? if so, read from memory and store into a, else: throw java.lang.ArrayIndexOutOfBoundsException
byte c = b[2]; // VM: is "2" equal or more than 0, is "2" less than 3 ? if so, read from memory and store into a, else: throw java.lang.ArrayIndexOutOfBoundsException

Server VM can optimize that away, but is only included in the SDK, not the JRE.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline PeterB

Junior Devvie





« Reply #6 - Posted 2005-10-23 20:29:46 »

ah, of course. It takes fewer operations to fetch the values from member variables as these checks don't have to be done.

Vault101 / Mace The Game
There are 10 kinds of people in the world. Those who understand binary and those who don't.
Offline Niwak

Senior Devvie


Medals: 1
Projects: 1



« Reply #7 - Posted 2005-10-23 21:11:42 »

I use vecmath classes for the general purpose usage but when it comes to rendering part, you have to fill one of the missing feature of the java language ; struct (i.e. Point3f[] that would not be stored as an array of pointers but as a continuous block of memory, compatible with other languages).

In my engine, I have a few wrapper classes that implements a generic interface IDataArray<> for struct arrays. The wrapper helps for access to the data and for the type of storage (NIO or java array, but you could imagine mapped OpenGL VBO for example).

This type of architecture is slower when accessed from java but way faster when accessed from native code so it depends on your needs.

                 Vincent
Offline InfoRital

Junior Devvie





« Reply #8 - Posted 2005-10-24 20:54:57 »

So theoricaly what's the best way to represent 3D space in term of performance and memory usage ?
Many small objects or float/double array ?

Java In Games : Java Games Community for French Developers
Offline Vorax

Senior Devvie


Projects: 1


System shutting down in 5..4..3...


« Reply #9 - Posted 2005-10-24 21:37:49 »

NIO buffers and VBO's

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jeff

JGO Coder




Got any cats?


« Reply #10 - Posted 2005-10-24 22:02:16 »

To furtehr explain Vorax's comment.

The savings you get by putting the data directly in video memory and not hacing to copy it around the system far outwiegh these other issues.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline zero

Junior Devvie





« Reply #11 - Posted 2005-10-26 08:13:44 »

Not much can outperform field-access and array-access is definitly slower.

Just curious, how about these 3 variations:

of course the following should be the fastest and it doesn't consume much memory:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
class JVector3 {
  private int x;
  private int y;
  private int z;

 public float getX() {
    return x;
 }

 public void setX(float  val) {
     x = val;
 }
 ..
}


but using a NIO Floatbuffer, ich have to call the put method 3 times for every JVector3, having a float array a single method call is sufficient. This coudl be a slow down using dymanic geometry for graphics applications.

Therefore, the next variation uses a float array and because all members are private and final the array size cannot change after constructing. The same is true for the elments using final lookups. As a result the JVM should be able to do the bounds check ones while constructing.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
class JVector3 {
  private final float[] data;
  private final int x;
  private final int y;
  private final int z;

 public float getX() {
    return data[x];
 }

 public void setX(float  val) {
     data[x] = val;
 }
 ..
}


Finally, using a single index in the code and again all fields are private aures that the elemts of a JVector3 are in a neighbours in the memory. (Asuming the JVM puts the array into a single region). Furhtermore a JVM, could cache x+1, x+2 for a fast access.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
class JVector3 {
  private final float[] data;
  private int final x;

 public float getY() {
    return data[x+1];
 }

 public void setY(float  val) {
     data[x+1] = val;
 }
}



just my random thoughts on it, don't really know whether they are right..
Offline PeterB

Junior Devvie





« Reply #12 - Posted 2005-10-26 19:04:37 »

ha ha, zero you should script several million runs of operations on each and post out the results!

Vault101 / Mace The Game
There are 10 kinds of people in the world. Those who understand binary and those who don't.
Offline zero

Junior Devvie





« Reply #13 - Posted 2005-10-27 10:17:34 »

ha ha, zero you should script several million runs of operations on each and post out the results!

You missed my point, in general the first variation always should be the best, the other both may used as a bridge to a vertexbuffer, through an array which can be stored in a FloatBuffer with one call. if possible only one read and write operation should be done druing an update.

Hey, why not have mutliple implementations of a vector class ? That is what OOP is for  Wink


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public abstract class Vector2 {
  public abstract float getX();
  public abstract float getY();

  public abstract void setX(float val) throws UnsupportedOperationException;
  public abstract void setY(float val) throws UnsupportedOperationException;

  public <T extends Vector2> add(Vector2 right, T sum) {
     sum.setX(this.getX() + right.getX());
     sum.setxYthis.getY() + right.getY());
     return sum;
  }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public class StructVector2 extends Vector2 {
 public float x;
 public float y;

  public float getX() { return this.x; }
  public float getY() { return this.y; }

  public void setX(float val) { this.x = val; }
  public void setY(float val) { this.y = val; }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public class ArrayVector2 extends Vector2 {
 private final float[] data;
 private final int x;

  public ArrayVector2(float[] data, int x ) {
    this.data = data;
    this.x = x;
  }

  public float getX() { return this.data[x]; }
  public float getY() { return this.data[x+1]; }

  public void setX(float val) { this.data[x] = val; }
  public void setY(float val) { this.data[x+1] = val; }
}


Offline Jeff

JGO Coder




Got any cats?


« Reply #14 - Posted 2005-10-28 00:25:16 »

Writes to native direct byte buffers are optimized out to straight memory access, no method call.
(At least in server VM.)

Thats part of the magic that gets us C speed acessing native memory.


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #15 - Posted 2005-10-28 09:27:24 »

With my very limited C knowledge, I wrote a vertex-array transformer (with 4x4 matrix) using pointer arithmetic.

Even then the Sun Server VM blew the C application away. Java code was 50% faster. I used "-O2 -6" as optimisation parameters in the free Borland compiler, and didn't use SSE/SIMD, so that might have been the catch. Anyway, I was surprised Smiley

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline Mark Thornton

Senior Devvie





« Reply #16 - Posted 2005-10-28 10:26:55 »

Even then the Sun Server VM blew the C application away. Java code was 50% faster. I used "-O2 -6" as optimisation parameters in the free Borland compiler, and didn't use SSE/SIMD, so that might have been the catch. Anyway, I was surprised Smiley
The JVM has probably used the SSE scalar operations which are much faster than the ordinary FP instructions that I suspect the Borland compiler used.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (57 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!