What’s more performant a Tuple<int, int> or a generic class as follows:
public class Interval<T>
{
public T From, To;
public static Interval<T> Create(T from, T to)
{
Interval<T> range = new Interval<T>();
range.From = from;
range.To = to;
return range;
}
}
}
Usage scenario – large collections (100K++) of these objects get filtered using LINQ. I currently have generic classes, thinking of moving to something more fat-free and hopefully faster. Wonder if I I’ll gain speed by switching to use Tuple‘s?
If you would look at the compiled code for a
Tuple<int, int>and anInterval<int>, it would be hard to tell them apart.The way that they are stored in memory will be identical.
You can also consider using a
structinstead of aclass:Structs have a value semantics, so the usage would be different in some cases. It is not as simple to implement a well working struct (e.g. implementing relevant comparison and conversion), but it would save you a lot of memory. A class has an overhead of about 12 or 24 bytes (depending on the platform), which you don’t get with a struct.