Distinguish between multiple types that are essentially Int32?

I suspect I can explain the performance problem with finding values in a List.

I suspect I can explain the performance problem with finding values in a List - you aren't implementing IEquatable, so any Equals call has to box the value it's comparing with. You should also implement IComparable for comparisons. // Please don't use camelCased type names... // I'd probably use UserId, CustomerId etc, partly to avoid non-interfaces // starting with I.

Public struct IdUser : IComparable, IComparable, IEquatable { ... public bool Equals(IdUser other) { return m_Main == other. M_Main; } public int CompareTo(IdUser other) { return m_Main. CompareTo(other.

MMain); } } You may well find that just this change brings the performance to an acceptable level. It won't help when populating the list, admittedly - but it'll depend on what your required operations are. Note that in most cases I'd expect the bulk of the performance hit to be transferring data from the database to start with; comparing the performance of populating a list just with in-memory data isn't really a realistic test of the performance impact on your real application.

Thanks for your response, I've now implemented both of those interfaces, and an increase in performance was seen. It is now taking just over 3 times longer on the new struct (rather than 5 times). So it's a step in the right direction.Thanks.

– Rich S Nov 12 '11 at 9:42 @RichS: Check the last bit of my answer though - how much are you micro-benchmarking code which doesn't form the bulk of your time? (Maybe this really is important - but I wouldn't be surprised to hear that really the database is the bottleneck.) Also, can you post your benchmarking code? I assume you're not running it in the debugger, btw?

– Jon Skeet Nov 12 '11 at 9:44 yeah, I appreciate that this test isn't completely realistic, and I wouldn't look to increase the performance by 0.001% when the read from the database will take the main chunk of the work. However, once I have data read in, I then do a lot of data manipulation. The examples I'm giving of idUser etc.. were just examples.

(did you see the comment regarding 'enums'? Very strange...) – Rich S Nov 12 '11 at 9:57 @RichS: That suggests your benchmarking code may not be quite right. Personally I'm not sure I'd use enums for this, as it's not really idiomatic - it's not like you really have an enumerated type.

– Jon Skeet Nov 12 '11 at 10:01.

Very interesting.. I nearly dismissed your comment at first, then looked at it again. I have created an enum with the syntax public enum idUser : int { } and run the performance test on that.. would you believe that it's actually quicker than the Int32 version? I need to go back and check this!

– Rich S Nov 12 '11 at 9:46.

The performance issue appears to be in doing the following: Creating a List populating it, then checking for the existance of some of the keys.. This is almost certainly due to the fact that your type doesn't implement IEquatable, resulting in boxing when equality comparisons are made. Implement this interface and check if this improves things. List relies on EqualityComparer.

Default to perform equality comparisons (say when you call Contains). Now this comparer tries to use the Equals(T other) method from IEquatable if possible, which avoids boxing value-types. If the type doesn't implement the interface, it falls back to object.

Equals(object obj), which requires value-types to be boxed. Of course, the only way to deal with all performance issues is to benchmark and profile. On another note, are you sure you don't want to use a set, such as HashSet rather than a list?

Calling List. Contains in a loop is strongly suggestive of you requiring set-semantics rather than list-semantics.

Thanks for your comment, I have implemented the change that you both suggested, and saw an increase in speed. My choice for a List was purely for testing purposes. – Rich S Nov 12 '11 at 10:01.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions