0

I'm working on a game, in one class I have for now 4 flags (I use boolean), but I'd like to switch to int and make each flag a bit. Some of my flags are checked inside the main loop every time, so looking to the future (where I could have more flags): will my flagset slow down my program? Is it better to keep using boolean?

8
  • 6
    Do you have some reason to believe that the performance of your game will hinge on how a bitflag is implemented? Write your code cleanly first, then profile, and only optimize after you profile. Commented Dec 10, 2015 at 15:26
  • 5
    This is a classic case of premature optimization. Bitwise operators and boolean checking are so insanely fast as to be irrelevant unless you're doing hundreds of millions of them a second. At which point...almost anything else you're doing is going to be costing more time. Commented Dec 10, 2015 at 15:27
  • @NicolBolas In one method I have to compare the boolean, and in the other I have to get the value of the flag and compare it. I'm trying to write as efficient as possible code. Downvoters pls explain... Commented Dec 10, 2015 at 15:28
  • 3
    @RomanHudylko: "I'm trying to write as efficient as possible code." Why? Do you think it'll be worth it? That's what I asked: is this case an actual performance problem? Otherwise, do it whatever way makes the code easier to read. The time it took you to write this question is probably longer than the number of ms that any response would actually save your application. Don't spend time making code faster in negligible areas. Commented Dec 10, 2015 at 15:30
  • 1
    I have been programming for more than 30 years. On at least two occasions I have managed to correctly guess where the performance problem was before I actually measured. Commented Dec 10, 2015 at 17:25

2 Answers 2

3

Optimization should hing only on how often you are actually going to use something. This is a big killer on programming efficiency. The way to determine IF you need to optimize something is figure out how often it will actually be used.

So, if you are only going to use something once or twice a second or when the user manually does something then the answer is most likely going to be "don't bother". On the other hand if the optimization is in something that runs continuously a thousand times a second then the answer is "maybe". The later depends on how many clocks the operation is eating. The case of flag checking the answer is most likely no as the clock ticks for either are negligible. The better optimization might be on why you are calling that function, routine, etc ... so many times to begin with.

Sign up to request clarification or add additional context in comments.

Comments

1

It sounds as if you are quite a beginner. So here is what I'd advice you to do: Program your game and think about optimization only when you notice something like lagging. Otherwise, it's not that important. Focus on other things, such as readability and reusability of code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.