Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  Enums !  (Read 3669 times)
0 Members and 1 Guest are viewing this topic.
Offline Chromanoid

Junior Member


Medals: 3



« Reply #30 - Posted 2011-10-10 04:24:55 »

TreeSet is not as robust as HashSet and EnumSet (contains throws ClassCastException and NullPointerException).

Improved caliper benchmark (just expanded http://code.google.com/p/caliper/source/browse/trunk/examples/src/main/java/examples/EnumSetContainsBenchmark.java).

Results:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
      setMaker trial   ns linear runtime
      TREE_SET     0 49,7 ======================
      TREE_SET     1 38,6 =================
      TREE_SET     2 49,8 ======================
LARGE_TREE_SET     0 67,4 ==============================
LARGE_TREE_SET     1 58,3 =========================
LARGE_TREE_SET     2 67,0 =============================
      HASH_SET     0 30,8 =============
      HASH_SET     1 31,0 =============
      HASH_SET     2 30,9 =============
LARGE_HASH_SET     0 39,2 =================
LARGE_HASH_SET     1 39,2 =================
LARGE_HASH_SET     2 39,2 =================
      ENUM_SET     0 16,6 =======
      ENUM_SET     1 16,6 =======
      ENUM_SET     2 16,6 =======
LARGE_ENUM_SET     0 15,9 =======
LARGE_ENUM_SET     1 15,9 =======
LARGE_ENUM_SET     2 15,9 =======


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  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
import com.google.caliper.Param;
import com.google.caliper.Runner;
import com.google.caliper.SimpleBenchmark;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

public class EnumSetPerformanceTest extends SimpleBenchmark {

    @Param
    private SetMaker setMaker;

    public enum SetMaker {

        TREE_SET {

            @Override
            Set<?> newSet() {
                return new TreeSet<RegularSize>(EnumSet.allOf(RegularSize.class));
            }

            @Override
            Object[] testValues() {
                return new Object[]{RegularSize.E1, RegularSize.E2, RegularSize.E20,
                            RegularSize.E32, RegularSize.E31};
            }
        },
        LARGE_TREE_SET {

            @Override
            Set<?> newSet() {
                return new TreeSet<LargeSize>(EnumSet.allOf(LargeSize.class));
            }

            @Override
            Object[] testValues() {
                return new Object[]{LargeSize.E1, LargeSize.E63, LargeSize.E64,
                            LargeSize.E65, LargeSize.E140};
            }
        },      
        HASH_SET {

            @Override
            Set<?> newSet() {
                return new HashSet<RegularSize>(EnumSet.allOf(RegularSize.class));
            }

            @Override
            Object[] testValues() {
                return new Object[]{RegularSize.E1, RegularSize.E2, RegularSize.E20,
                            RegularSize.E32, RegularSize.E31};
            }
        },
        LARGE_HASH_SET {

            @Override
            Set<?> newSet() {
                return new HashSet<LargeSize>(EnumSet.allOf(LargeSize.class));
            }

            @Override
            Object[] testValues() {
                return new Object[]{LargeSize.E1, LargeSize.E63, LargeSize.E64,
                            LargeSize.E65, LargeSize.E140};
            }
        },
        ENUM_SET {

            @Override
            Set<?> newSet() {
                return EnumSet.allOf(RegularSize.class);
            }

            @Override
            Object[] testValues() {
                return new Object[]{RegularSize.E1, RegularSize.E2, RegularSize.E20,
                            RegularSize.E32, RegularSize.E31};
            }
        },
        LARGE_ENUM_SET {

            @Override
            Set<?> newSet() {
                return EnumSet.allOf(LargeSize.class);
            }

            @Override
            Object[] testValues() {
                return new Object[]{LargeSize.E1, LargeSize.E63, LargeSize.E64,
                            LargeSize.E65, LargeSize.E140};
            }
        };

        abstract Set<?> newSet();

        abstract Object[] testValues();
    }

    private enum RegularSize {

        E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17,
        E18, E19, E20, E21, E22, E23, E24, E25, E26, E27, E28, E29, E30, E31, E32
    }

    private enum LargeSize {

        E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17,
        E18, E19, E20, E21, E22, E23, E24, E25, E26, E27, E28, E29, E30, E31, E32,
        E33, E34, E35, E36, E37, E38, E39, E40, E41, E42, E43, E44, E45, E46, E47,
        E48, E49, E50, E51, E52, E53, E54, E55, E56, E57, E58, E59, E60, E61, E62,
        E63, E64, E65, E66, E67, E68, E69, E70, E71, E72, E73, E74, E75, E76, E77,
        E78, E79, E80, E81, E82, E83, E84, E85, E86, E87, E88, E89, E90, E91, E92,
        E93, E94, E95, E96, E97, E98, E99, E100, E101, E102, E103, E104, E105, E106,
        E107, E108, E109, E110, E111, E112, E113, E114, E115, E116, E117, E118,
        E119, E120, E121, E122, E123, E124, E125, E126, E127, E128, E129, E130,
        E131, E132, E133, E134, E135, E136, E137, E138, E139, E140,
    }
    private Set<?> set;
    private Object[] testValues;

    @Override
    protected void setUp() {
        this.set = setMaker.newSet();
        this.testValues = setMaker.testValues();
    }

    public void timeContains(int reps) {
        for (int i = 0; i < reps; i++) {
            set.contains(testValues[i % testValues.length]);
        }
    }

    public static void main(String... args) {
        Runner.main(EnumSetPerformanceTest.class, new String[]{"-Dsize=100,1000,10000,100000",
                    "--trials", "3"});
    }
}
Offline nsigma
« Reply #31 - Posted 2011-10-10 11:08:57 »

Ooh, I love micro-benchmarks!  Tongue

unfortunately even in 6u26 it suffers from the interface overhead.

Why assign them to Set rather than their concrete types if you want to test their comparative speed?

EnumSets are the one time I don't use the general convention of using the interface for fields and method parameters anyway - using EnumSet seems somehow more semantically correct to me.

I see it allocates in bytecode / the JDK source, so it makes me wonder what magic happens and also, what Android does (ie, how Android fscks this up).

Don't forget that EnumSets are mutable, so you can get away with allocating once in a lot of cases if you feel the need (I don't, but then I'm not targeting Android!  Smiley )

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline drakesword

Junior Member


Medals: 1



« Reply #32 - Posted 2011-10-10 15:00:32 »

I remember working on reverse engineering the communications of a game. Some objects in the game allowed the user to interact with them and a random numbr was generated. When we started digging into to communications we found that the number was being generated client side then was sent to the server so all of the clients would see the result. In this situation they were not using enums and an invalid number did one of two things, A) if set to 0 the animation for that item kept going. B) if set to more then 8 or less then 0 the item would crash all clients viewing that item.

All in all it was very entertaining to watch the problems produced by poor coding (no data verification nor range checking of the client)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #33 - Posted 2011-10-11 21:44:39 »

I don't like enums in Java, because I think they missed the point. I just want a numerical constant, man.

The whole point of enums is that they're NOT numerical constants and that each set of enums is a distinct type from each other.  This is why I can't set my direction to COLOR_RED or my color to NORTH.  And if you're going to introduce distinct types in Java, you're going to do it with classes, unless you're keen on creating a parallel type system that only works for enums.

I'm just saying, Java enums take so long to write I might as well just make a class. I just want MyEnum = enum { A, B, C, D} and then I can just say MyEnum.A whenever. It's good to know that Java enums become ints under the hood, but for me enums are completely convenience anyway - when I can just use an int or a string I type all over my code if I want. Something that's supposed to be convenient shouldn't be so inconvenient.

See my work:
OTC Software
Offline Nate

JGO Kernel


Medals: 128
Projects: 3
Exp: 14 years


Esoteric Software


« Reply #34 - Posted 2011-10-11 22:05:47 »

I'm just saying, Java enums take so long to write I might as well just make a class. I just want MyEnum = enum { A, B, C, D} and then I can just say MyEnum.A whenever.

I don't get it? It works nearly as you typed.

1  
2  
3  
4  
enum MyEnum { A, B, C, D }
public static void main (String[] args) throws Exception {
   System.out.println(MyEnum.A);
}

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #35 - Posted 2011-10-11 22:06:28 »

I'm just saying, Java enums take so long to write I might as well just make a class. I just want MyEnum = enum { A, B, C, D} and then I can just say MyEnum.A whenever.

I don't get it? It works nearly as you typed.

1  
2  
3  
4  
enum MyEnum { A, B, C, D }
public static void main (String[] args) throws Exception {
   System.out.println(MyEnum.A);
}

Maybe I lie.

See my work:
OTC Software
Offline Catharsis

Junior Member




EGR Software rocks!


« Reply #36 - Posted 2011-10-14 03:49:02 »

I'm a huge fan of enums and particularly extensible enums.  One of the problems of extensible enums is that you can't use them in EnumSet. EnumSet is necessary to perform set operations efficiently otherwise things get particularly horrendous especially on Android. I created a new collection ExtEnumSet that is like EnumSet, but supports extensible enums. I wrote more here:

https://plus.google.com/117904596907381647757/posts/aHiaoA8f6WZ

I've moved to the ExtEnumSet for storing entity state in my ES.

Another problem with EnumSet is that under the hood it keeps track of a unique array of Enums for the given supported type for each EnumSet. Ahh.. This may be the case for Java 5, but I just checked in Java 6 source and it looks like this array is universally shared which wasn't the case in Java 5 if I recall correctly. I actually finally had to move to Java 6 due to javac / compiler issues with some sort of generic method failure with Java 5.

Anyway.. I'm a huge fan of extensible enums and they were the final missing key to really unlock state tracking between unrelated components in the component architecture I'm releasing soon; useful in so many ways. Also it's quite easy to use enums extensible or not as components in my component architecture. I yap on a bit about that in the long ES topic on B^3's blog:
http://t-machine.org/index.php/2011/08/22/entity-system-rdbms-beta-a-new-example-with-source/comment-page-1/#comment-30180

[edit] hah, that link to the ES comment is silly Adam / B^3 ruffled my feathers slightly, so I kind of go a little off in that reply.. It's the one where I described using enums as components though.  Shocked  The whole thread is a fairly spirited discussion of sorts..

Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org/
http://www.egrsoftware.com/
Pages: 1 [2]
  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.

xsi3rr4x (26 views)
2014-04-15 18:08:23

BurntPizza (21 views)
2014-04-15 03:46:01

UprightPath (37 views)
2014-04-14 17:39:50

UprightPath (19 views)
2014-04-14 17:35:47

Porlus (35 views)
2014-04-14 15:48:38

tom_mai78101 (61 views)
2014-04-10 04:04:31

BurntPizza (119 views)
2014-04-08 23:06:04

tom_mai78101 (219 views)
2014-04-05 13:34:39

trollwarrior1 (186 views)
2014-04-04 12:06:45

CJLetsGame (193 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!