Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6819191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:16:35+00:00 2026-05-26T21:16:35+00:00

I am implementing a 2-player game that will be run in a tight loop

  • 0

I am implementing a 2-player game that will be run in a tight loop literally hundreds of thousands of times, being then performance paramount.

My code actually looks something like this:

public class Table {
    private final int WHITE_PLAYER = +1;
    private final int BLACK_PLAYER = -1;

    private final int currentPlayer;
    private final int otherPlayer;

    ...
}

I was wondering if I would get any performance hit would I choose to replace

private final int WHITE_PLAYER = +1;
private final int BLACK_PLAYER = -1;

to an enum defined as

public enum Players {
    WhitePlayer,
    BlackPlayer
}

I had the idea that enums were just syntactic sugar over integer constants, and taking a glaze look over the bytecode generated for a test enum, as well as the code calling it, seems to indicate that using them is indeed the same as making a static method call but for some enum infrastructure that is set up when it’s first run.

Is my assumption that it is indeed the same to use enums as static constants correct or am I missing something here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:16:36+00:00Added an answer on May 26, 2026 at 9:16 pm

    In a micro-benchmark, yes, checking integer constant equality will be faster than checking enum constant equality.

    However, in a real application, let alone a game, this will be totally irrelevant. The things that are happening in the AWT subsystem (or any other GUI toolkit) dwarf these micro-performance considerations by many orders of magnitude.

    EDIT

    Let me elaborate a little then.

    An enum comparison goes like this:

    aload_0
    getstatic
    if_acmpne
    

    An integer comparison for a small integer goes like this:

    iload_0
    iconst_1
    if_icmpne
    

    Obviously, the first is more work than the second, although the difference is quite small.

    Run the following test case:

    class Test {
    
        static final int ONE = 1;
        static final int TWO = 2;
    
        enum TestEnum {ONE, TWO}
    
        public static void main(String[] args) {
            testEnum();
            testInteger();
            time("enum", new Runnable() {
                public void run() {
                    testEnum();
    
                }
            });
            time("integer", new Runnable() {
                public void run() {
                    testInteger();
                }
            });
        }
    
        private static void testEnum() {
            TestEnum value = TestEnum.ONE;
            for (int i = 0; i < 1000000000; i++) {
                if (value == TestEnum.TWO) {
                    System.err.println("impossible");
                }
            }
        }
    
        private static void testInteger() {
            int value = ONE;
            for (int i = 0; i < 1000000000; i++) {
                if (value == TWO) {
                    System.err.println("impossible");
                }
            }
        }
    
        private static void time(String name, Runnable runnable) {
            long startTime = System.currentTimeMillis();
            runnable.run();
            System.err.println(name + ": " + (System.currentTimeMillis() - startTime) + " ms");
        }
    }
    

    and you will find that the enum comparison is slower that the integer comparison, on my machine by around 1.5%.

    All I was saying is that this difference will not matter in a real application (“Premature optimization is the root of all evil”). I deal with performance problems on a professional basis (see my profile) and I have never seen a hot spot that could be traced to something like this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails app implementing a game, so it's expected that a player
What is a good architecture for implementing a real-time, multi-player Silverlight-based game that is
I have an air application that I'd like to turn into a multi-player game.
Already finished implementing the player. I want to implement the progress bar. But I
Im implementing a pool billiard game in Java and it all works fine. It
I'm implementing a custom video player because I need custom video controls. I have
I'm considering trying to make a game that takes place on an essentially infinite
I'm implementing a board game for a course I'm taking. There are 5 racks
How would I go about implementing an inventory system for a game in PHP?
I'm working on a puzzle game in Flash. It's a two-player, head-to-head game with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.