Array Performance

  • Thread starter Thread starter Stuart
  • Start date Start date
S

Stuart

I have a Struct like this:

public struct SocketStruct
{

public int SerialNo1;
public long SerialNo2;
public int status;
public byte[] SomeData;

}

I need to put this into an array, and retrieve it with either SerialNo1
or SerialNo2, to get the other values.

Performance is the key issue - whats the most efficient way of doing it?
A Sorted list?

Stuart.
 
Stuart said:
Performance is the key issue - whats the most efficient way of doing it?
A Sorted list?

An IDictionary with constant-time lookup would be preferrable, that
means a System.Collections.Hashtable is your best bet. SortedList has
horrible performance, since inserts and removes effectively moves all
the following items.

Note, that your struct will be boxed when using the .NET
data-structures, Perhaps you would want SocketStruct to be a class? What
is your rationaly for having it as a value-type?

Since you apparently have two keys, you would need to double-index,
using two Hastables, (you would also need two sortedlists :).

Somewhere along the lines of:

public class Sockets {
IDictionary BySerial1;
IDictionary BySerial2;
public Sockets(IDictionary d1, IDictionary d2) {
this.BySerial1 = d1;
this.BySerial2 = d2;
}
public Sockets(): this(new Hashtable(), new Hashtable()) {}

void Add(SocketStruct s) {
BySerial1.Add(s.SerialNo1, s);
BySerial1.Add(s.SerialNo2, s);
}
SocketStruct LookupSerial1(int serial1) { return BySerial1[serial1]; }
SocketStruct LookupSerial2(long serial2) { return BySerial2[serial2]; }

void RemoveSerial1(SocketStruct s) {
BySerial1.Remove(s.SerialNo1);
BySerial2.Remove(s.SerialNo2);
}
}

Remember to synchronize access to the Sockets instance if you are
multithereading.
 
Note, that your struct will be boxed when using the .NET
data-structures, Perhaps you would want SocketStruct to be a class? What
is your rationaly for having it as a value-type?

I assumed a struct would be more efficient, given the simplicity of the
data within it.

Since you apparently have two keys, you would need to double-index,
using two Hastables, (you would also need two sortedlists :).

Somewhere along the lines of:

[SNIP]

Thanks, I will give that a try.

Stuart.
 
Stuart said:
I assumed a struct would be more efficient, given the simplicity of the
data within it.

That's a dangerous assumption. I think you should read up on the
differences between value types and reference types, and understand
boxing.
 
Back
Top