eventargs - performance question

  • Thread starter Thread starter Piotrekk
  • Start date Start date
P

Piotrekk

Hi

I have created event to use it when user is pressing a button on the
joystick. In the parameters I need to pass states of all buttons. I
will need to call event subscribers each 1ms. For this purpose I have
created the following class.

class StateChangedEventArgs : EventArgs
{
static class States
{
public static byte x, y, select, start, a, b, c, d, z1,
z2;

//ButtonType is enum
public static ButtonType btn;
}

// PROPERTIES TO GET SET States values HERE

//constructor of event class
public StateChangedEventArgs(args)
{
// SET CURRENT VALUES
}
}

Delegate and event are:

private delegate void StateChanged(object sender,
StateChangedEventArgs e);
private event StateChanged a;


USAGE:

// called each 1ms
{
a(args);
}

Then the question. What do you think about performance of such an
operation. Is this ok that I create StateChangedEventArgs instance
each 1ms? I have made "States" internal class as static to avoid
creating to much data each 1ms. What are the good practics?

Kind Regards
Piotr Kolodziej
 
Then the question. What do you think about performance of such an
operation. Is this ok that I create StateChangedEventArgs instance
each 1ms? I have made "States" internal class as static to avoid
creating to much data each 1ms. What are the good practics?

Well currently there's no point in creating an instance of
StateChangedEventArgs at all, given that you don't store any
information in it. You should keep the actual information within
StateChangedEventArgs, rather than having all the information
statically.

Creating 1000 objects every second is child's play for the GC.

As for best practices, the core best practice you're missing here is to
design simply to start with, only bending the natural design out of
shape to micro-optimise it when you've *proven* that you've got a
performance issue.
 
Back
Top