Trade off question

I’m writing some code that uses a triplet of integers which new integers are created fairly often. Currently I have them written as a class with 3 elements. This makes it really easy to use and easier to understand. However, I don’t know what the creation of this small class actually encumbers, and I don’t know how much extra garbage collection it creates. So I’ve come up with a clever alternative, where instead of using a class, I just use a single integer. I know each of the 3 ints will be small ranges, and will fit in 2 digits, so I figured I could use a 6 digit number and use divisions to separate them. This code seems like it would take less memory and time to implement, though it’s much harder to understand – should I not look at it for 6 months, I might not remember what the hell I was doing. Of course I can always comment away and hope it’s understandable later. So, if these triplets are created often enough, is it really worth it to make the code a little more complicated?

Premature optimization is the root of all evil.

First get your program to work. Then you can look for performance bottlenecks. It is better to have a clear design than a “clever” one.

Yea, it’s already working, that’s why I’m looking into optimizing it. But I don’t know how java implements these things, so I need some input from others.

Have you profiled your code? Do you know for a fact that optimising this bit is worthwhile?

No I don’t know, that’s the whole point of my question :\

Noone can tell you if your code is fast or not if you don’t profile it. Doesn’t sound like it matters at all though.

so how do you profile your code :slight_smile:

determine what is slowest machine you want your game to be able to run on, find that machine and try to run it :slight_smile:
if your game is too slow, profile it with some profiler, netbeans profiler is said to be the best.

Ok, I think I’ll just leave it how it is. Really was trying to figure out if constantly creating this class of 3 ints was creating a lot of extra overhead, but since nobody jumped out and said “holy crap that’s the most inefficient thing I’ve ever heard!”, I imagine it’s not going to cause a problem. Thanks.

oh yeah, it may not be slow, but why are you constantly creating that class? Couldn’t you create it once and then reuse it instead of creating another?

Wow, can’t believe I hadn’t even thought of that, it’s so simple! I’ll have to change a couple small things to ensure they don’t overlap, but that’s definitely a huge improvement, thanks kova!