Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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  
  Switches and fake indexing  (Read 1473 times)
0 Members and 1 Guest are viewing this topic.
Offline zingbat

Senior Duke




Java games rock!


« Posted 2006-10-10 22:43:19 »

I was looking at this section of the vm spec:
http://java.sun.com/docs/books/vmspec/html/Compiling.doc.html#4095

and realized there are two methods to resolve a switch. That is tableswitch and lookupswitch. Tableswitch is good for when the switch cases are sequencial numbers like this (0,1,2,3), which happen to be the indices of a 4D vector.

Since I have recently restarted some work on a vecmath api i was planning i was thinking about doing something like this, using switches to fake array indexing. The float4D class is like Vector4d except that atributes are private and the indexing methods.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
class float4D {
  private float _0;
  private float _1;
  private float _2;
  private float _3;
  ... constructors ...
  public float get0() { return _0; }
  public float get1() { return _1; }
  public float get2() { return _2; }
  public float get3() { return _3; }
  public void set0(float f) { _0=f; }
  public void set1(float f) { _1=f; }
  public void set2(float f) { _2=f; }
  public void set3(float f) { _3=f; }
  public float get(int i) { switch (i) {
                              case 0: return _0;
                              case 1: return _1;
                              case 2: return _2;
                              case 3: return _3;
                              case 4: return _4;  
                              default: return 0 } }
  public void set(int i,float f) { switch (i) {
                              case 0: _0=f;  
                              case 1: _1=f;
                              case 2: _2=f;
                              case 3: _3=f;
                              case 4: _4=f;  } }
  ... more stuff ...
}


What do you guys think of this class? Would it be useful to have these indexing methods? Any idea if the speed match to array indexing?
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #1 - Posted 2006-10-11 14:35:50 »

Simply using an array is most likely faster than a switch, but if I were you I'd create some test cases and profile them before trying out things like this, just to see if it's worth bothering at all.

Online Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2006-10-11 19:23:52 »

Not to mention the hassle of working with such a class...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
float4 a = new float4();
float4 b = new float4();
float4 c = new float4();

// this
c.set(0, a.get(0) + b.get(0));
c.set(1, a.get(1) + b.get(1));
c.set(2, a.get(2) + b.get(2));

// as opposed to
c.x = a.x + b.x;
c.y = a.y + b.y;
c.z = a.z + b.z;



What would be the advantage? Really... what did you have in mind.

Switches are just tightly-packed jump-tables, you will (on average) have a few if's every lookup, that's more costly than no lookup (a field)


A little benchmark:
on client VM 1.5 it is factor 1250% slower (!)
on server VM 1.5 it is factor 47% slower

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zingbat

Senior Duke




Java games rock!


« Reply #3 - Posted 2006-10-11 22:25:02 »

I supose its not a big deal if i just write four instructions instead of using a loop:

1  
2  
3  
4  
v.set0(x)
v.set1(y)
v.set2(z)
v.set3(w)


Does anyone know if its still worth to use fields instead of arrays for vectors?

This would simplify a lot of things:
1  
2  
3  
class floatTuple {
  float[] components;
}


Im making a python script to generate java source for tuples similar to the java.vecmath ones. Possible primitive types are byte, short, int, long, float, double combined with dimensions 1D,2D,3D,4D,9D (for 3x3 matrices), 16D (for 4x4 matrices). This gives a total of 6x6=36 different classes. What im doing is just using python to do what java templates don't do.

Each class supports basic tuple operations +, -, neg, abs, combine, literals, slicing, conversion, coercing, construction, import/export data from/to buffers and arrays, etc. All classes final for fastest access.

For linear algebra operations these would in a LinAlg final class factory with a method: LinAlg la = LinAlg.default() that works just like Math. Another class i would have to generate to work with 36 different tuples.

1  
2  
3  
4  
5  
6  
7  
LinAlg la = LinAlg.default();
float4D v1 = new float4D(1, 0, 0, 0);
float4D v2 = new float4D(0, 1, 0, 0);
float4D v3;
float16D m;
v3 = la.cross(v1,v2)
rot = la.makeRotMatrix(alpha,v1,beta,v2,gama,v3)


This stuff is almost like machine code instructions but it is the fast and better looking i can get, while keeping classes final.
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.

Longarmx (39 views)
2014-10-17 03:59:02

Norakomi (30 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (57 views)
2014-10-14 00:39:48

TehJavaDev (58 views)
2014-10-14 00:35:47

TehJavaDev (48 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!