views:

85

answers:

4

Hello everybody

A few problem about instances are that Equals method doesn't provide true if even they contain same values. So trying to override Equals method provide much more slower than reference equality.

While i was thinking point of performance, i thought that it is stupidly to create 2 instance which is same but not same memory adress. If i can avoid to create same instances with different references will increase performance and help to compare references will be more easy than write custom Equal methods.

For example:

I have Coordinate class Which hold chess board coordinates.So i need only Coordinate[8,8] array for represent all cordinates of board. Instead of create instances, i can create all instances then my factory method can return them.

Cooardinate.Get(2,3) instead of new Coordinate(2,3) First is static class's static method which returns pre-defined coordinate in given values.

Another advantage is that we will not spend time to create and collect garbage objects in memory. All of them is pre-defined already. Also we can provide unique GetHashCode for instances in easy and fast way like 0 for [0,0], 1 for [0,1]....

Isn't it worthy to try ? this idea would make coding harder write or understand ? Is there any such a pattern ?

Well, shortly what is disadvantages of this way ?

+1  A: 

In this instance where you're dealing with a chess board (so 64 total coordinates) you don't need to be overly concerned with performance or garbage collection. Having said that, I think holding the baseline 64 coordinates in a dictionary is just fine (and makes sense). In terms of the Equals() comparison, you're basically comparing 2 integer values which is going to be lightning fast so overriding the Equals() method to specify that comparison is the right approach.

Steve Michelotti
Cordinate class was just example for easy understand. I have also classes which hold many fields and it is possible to create all instances pre-defined.
Freshblood
A: 

If you really were running into performance problems because of having to do deep comparisons in order to determine equality then you might want to look at the Flyweight pattern, but as you're only talking about 64 pairs of smallints I think that would be complete overkill in this case. What you actually need is to override the Equals operator to compare the two coordinates - that will be plenty fast enough. Anything more complex than that is probably a false optimisation, at least on most normal platforms.

gkrogers
+2  A: 

This is a good solution and in certain situations can save you a lot of time and memory. The main drawback is that it gets really complicated if your objects are mutable. If that is not the case, then this is really not that bad. You just have to make sure that all instances are obtained from the same factory. You do not even have to create all the instances in advance, but can make the class create a new instance when a particular set of parameters is requested for the first time (basically lazy-loading).

Space_C0wb0y
+1 and deleted my post because this one is better.
Ladislav Mrnka
@Space_C0wb0y - mutable objects means instances which hold changeable fields ? By the way, my simple test shows that it is 2 times faster if object will be instantiated million times. Already my classes created millions times so it will give performance benefits and easy equality checking.
Freshblood
@Freshblood: Yes, mutable means they have changeable fields.
Space_C0wb0y
+1  A: 

The short answer is that the disadvantage of this approach is that it increases the complexity of your code, thereby making it more difficult to code correctly, more difficult to debug, and more difficult to maintain.

Therefore, as with all optimisations, you should only contemplate it if you have a genuine need to optimise (e.g. it's running way too slow, or using way too much memory), and if the reward (faster performance, smaller memory usage) outweighs the risks (spending time optimising when you could be doing something more useful, introducing a bug, not being able to find a bug, not being being able to modify the code in the future quickly and easily, or without introducing a bug).

gkrogers
@gkrogers: So, do you have short term memory, or did you answer twice intentionally?
Space_C0wb0y
@Space_C0wb0y: Both, but in this case I answered twice intentionally, after the OP clarified his question.
gkrogers