Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (563)
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  
  Enums vs ints  (Read 1444 times)
0 Members and 1 Guest are viewing this topic.
Offline c_lilian

Senior Member


Projects: 1


Java games will probably rock someday...


« Posted 2005-04-14 08:20:21 »

I've written a simple microbechmark (dont' flame me) to have a guess at the cost of using enums vs int constants.

so my enum is defined like this :

public enum Constants { E1(10), E2(20), E3(30)...;
  private final int value;
  Constants(int value){
         this.value = value;
  }
}

and the corresponding int constants are :
private static final int I1 = 10, I2 = 20, I3 = 30...

then two loops computing million times things like E1.value + E2.value + E3.value ...
and I1 + I2 + I3

----
now the results :
hostpot client : int constants are twice as fast as Enums.
hotspot server :
 - everything seems precalculated by hotspot (result time near 0) for int constants. I'll have to write a more tricky bench.
 - Enums are now 4 times faster (making them 2 times faster than the int constants of hostpot client).
==>
I know the poor value of that kind of benchmark, but it shows two things :

- enums are (for me) worth using it to replace int constants, in API like Open GL (thus adding type checking to GL commands).
- too bad hotspot server doesn't recognize that constant pattern (an enum IS constant and its member field IS final : should be optimized like those int constants).

Any thoughts ?

Lilian

Online princec

JGO Kernel


Medals: 380
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2005-04-14 08:37:30 »

1. Hurray for Enums!
2. You won't be able to use them for GL because the presence or non-presence of extensions changes the set of constants and combinations that can be used anyway.

Cas Smiley

Offline c_lilian

Senior Member


Projects: 1


Java games will probably rock someday...


« Reply #2 - Posted 2005-04-14 11:05:44 »

Well, with the java GL bindings i know, to use an appropriate extension, one has to use the integer constant generated (or written) by the binding tool.

So what's the difference with enums ? when an extension is added to the set of managed extensions, enums could be added altogether.

Let's look at a basic example :

gl.glClear(GL.GL_COLOR_BUFFER_BIT);

could be replaced by

public enum ClearConstants {
 COLOR_BUFFER_BIT(GL.GL_COLOR_BUFFER_BIT),
 DEPTH_BUFFER_BIT(GL.GL_DEPTH_BUFFER_BIT);
 ...
}
this would give us :
gl.clear(ClearConstants.COLOR_BUFFER_BIT);
or with static import
gl.clear(COLOR_BUFFER_BIT);

Then (another day), GL_STENCIL_BUFFER_BIT appears. It is added to the bindings as another integer constant and as an enum STENCIL_BUFFER_BIT(GL.GL_STENCIL_BUFFER_BIT) to the typesafe enums.

What's wrong with that ? (except the manual part of it : affecting a new GL constant to the right enum ?)

Lilian

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

JGO Kernel


Medals: 380
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #3 - Posted 2005-04-14 11:22:00 »

The issue is that the set of allowed constants is determined at runtime by GL, which means compile time checking is mostly a complexity best avoided. It's really, really complicated when you try to do it. The best you could do is put all of the GL constants in one GLenum class to prevent people using ints (there are a few API calls where it's easy to confuse the two and get strange results which are a pain to debug).

Cas Smiley

Offline Markus_Persson

JGO Wizard


Medals: 15
Projects: 19


Mojang Specifications


« Reply #4 - Posted 2005-04-14 12:17:06 »

Except sometimes you want to do things like GL_LIGHT0+4 and so, and enums would break that code.

Play Minecraft!
Online princec

JGO Kernel


Medals: 380
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #5 - Posted 2005-04-14 12:22:31 »

Aye, there's a few places where you do that kind of thing. So boo! for GL enums!

Cas Smiley

Offline c_lilian

Senior Member


Projects: 1


Java games will probably rock someday...


« Reply #6 - Posted 2005-04-14 12:39:10 »

Enums can have methods, like : LIGHT_O.add(4).

;-)

Lilian




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.

Grunnt (16 views)
2014-09-23 14:38:19

radar3301 (14 views)
2014-09-21 23:33:17

BurntPizza (31 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (29 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (75 views)
2014-09-10 13:57:51

TehJavaDev (105 views)
2014-09-10 06:39:09
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!