Micheal Hill
Micheal Hill

Reputation: 1659

Efficient temporary dataset

I have a class in C# that is storing information on a stack to be used by other pieces of the application later. The information is currently stored in a class (without any methods) and consists of some ints, shorts, floats and a couple of boolean values. I can be processing 10-40 of these packets every second - potentially more - and the information on the stack will be removed when it's needed by another part of the program; however this isn't guaranteed to occur at any definite interval. The information also has a chance of being incomplete (I'm processing packets from a network connection). Currently I have this represented as such:

public class PackInfo
{
    public boolean active;
    public float f1;
    public float f2;
    public int i1;
    public int i2;
    public int i3;
    public int i4;
    public short s1;
    public short s2;
}

Is there a better way that this information can be represented? There's no chance of the stack getting too large (most of the information will be cleared if it starts getting too big) but I'm worried that there will be a needless amount of memory overhead involved in creating so many instances of the class to act as little more than a container for this information. Even though this is neither a computationally complex or memory-consuming task, I don't see it scaling well should it become either.

Upvotes: 1

Views: 302

Answers (1)

Matthew
Matthew

Reputation: 25773

This sounds like it would be a good idea to use a generic Queue for storing these. I have the assumption that you're handling these "messages" in order.

As for the overhead of instantiating these classes, I don't think that instantiating 10-40 per second would have any visible impact on performance, the actual processing you do afterwards would likely be a much better candidate for optimization than the cost of instantiation.

Also, I would recommend only optimizing when you can actually measure performance in your application, otherwise you might be wasting your time doing premature optimization.

Upvotes: 4

Related Questions