N

N

  • NA
  • 15
  • 0

This is a performance issue: Struct vs. Class.

May 27 2009 4:09 PM
 

This is a performance issue: Struct vs. Class. I'm at that point in development where I need to make the app more stable. The program is a truck control and monitor system. Right now the app can have any number of trucks, let's say 5. Each has an engine, the shape of the information will be given later. The child form per truck has controls to change RPM, GEAR, and Pressure as well as gauges to monitor various other aspects of information being sent back to the form from the actual engine. Needless to say this makes for all trucks to be observable a little hard on one screen. This is where the truck monitor comes in, a smaller version with bear minimum, to have all trucks on screen.


I've designed where each truck gets a time slice since we're using RS-232. We're locked into this there is no negotiating this point. Each truck writes to, receives from the engine, process information, writes to file, and eventually send to monitor child form. All this occurs at 250 ms per truck.


Each truck has a monitor for mostly display and contains three controls.


As to the processing, each truck listen to incoming port, for properly formed string data, parses the data, assigns proper portion to the right variable of the engine and gauges. Such as, rpm info is place in engine.rpm_var and so on.


All this is accomplished through a loop on the parent form, MainController. This parent form has the comport portion,RS-232, here. The loop looks this:


Indiv_Truck_Cntrl tempChild = (Indiv_Truck_Cntrl)this.MdiChildren[trxx];

{

Truck_Active.Text = tempChild.Name;

//Performance_Log_File("from " + tempChild.Name, " tempChild.port_out = " + tempChild.port_out);

Main_Serial_Port1.Write(tempChild.port_out);


test_port = Main_Serial_Port1.ReadExisting();

//Performance_Log_File(tempChild.Name, " has in test_port = " + test_port);


if (test_port == "")

{

// commment

}

else

{

Send_Truck_Info(test_port);

}

tempChild.SEND_SETTINGS_Click(); // goes to Indiv_Truck rat here!!!!!


So the question here is do I use a class or a struct for the information I need, that is for Engine. Right now I have my Engine defined as:


public class Engine

{

private string c_TRUCK_ID;

public string TRUCK_ID //IS PART OF THE OUTPUT STREAM, SUGGESTED UDP/BROADCAST SYSTEM

{

get { return c_TRUCK_ID;} set { c_TRUCK_ID = value;}

}


private string c_TRUCK_NUM;

public string TRUCK_NUM //THIS IS ASSIGNED WHEN TRUCK IS RECOGNIZED/POLLED

{

get { return c_TRUCK_NUM;} set { c_TRUCK_NUM = value; }

}


private string c_TRUCK_IP;

public string TRUCK_IP //IP ADDRESS RIGHT HERE, COMES FROM INDIVIDUAL SERVER/TRUCK

{

get { return c_TRUCK_IP;} set { c_TRUCK_IP = value;}

}

public string RPM = "RPM";

private string c_RPM_var;

public string RPM_var

{

get { return c_RPM_var;} set { c_RPM_var = value;}

}

public string GEAR = "GEAR";

private string c_GEAR_var;

public string GEAR_var

{

get { return c_GEAR_var;} set { c_GEAR_var = value;}

}

private string c_GEAR_Name;

public string GEAR_Name

{

get { return c_GEAR_Name; } set { c_GEAR_Name = value; }

}

public string OIL_PRES = "OIL PRESSSURE ENG";

private string c_OIL_PRES_var;

public string OIL_PRES_var

{

get { return c_OIL_PRES_var;} set { c_OIL_PRES_var = value;}

}

public string COOLANT_TEMP = "COOLANT TEMP ENG";

private string c_COOLANT_TEMP_var;

public string COOLANT_TEMP_var

{

get { return c_COOLANT_TEMP_var;} set { c_COOLANT_TEMP_var = value;}

}

public string BAT_VOLT = "BATTERY VOLTAGE";

private string c_BAT_VOLT_var;

public string BAT_VOLT_var

{

get { return c_BAT_VOLT_var;} set { c_BAT_VOLT_var = value;}

}

public string TRANS_OUT_RPM = "TRANSMISSION OUTPUT RPM";

private string c_TRANS_OUT_RPM_var;

public string TRANS_OUT_RPM_var

{

get { return c_TRANS_OUT_RPM_var; } set { c_TRANS_OUT_RPM_var = value; }

}

public string FUEL_PRESS = "FUEL PRESSURE ENG";

private string c_FUEL_PRESS_var;

public string FUEL_PRESS_var

{

get { return c_FUEL_PRESS_var; } set { c_FUEL_PRESS_var = value; }

}

public string LOCKUP_CONV = "LOCKUP CONVERTER"; // transmission

public string c_LOCKUP_CONV_var;

public string LOCKUP_CONV_var

{

get { return c_LOCKUP_CONV_var; } set { c_LOCKUP_CONV_var = value; }

}

public string OVER_PRESS = "OVER_PRESS SETPOINT";

private string c_OVER_PRESS_var;

public string OVER_PRESS_var

{

get { return c_OVER_PRESS_var; } set { c_OVER_PRESS_var = value; }

}

public string FRAC_PRESS = "FRAC PRESSURE";

private string c_FRAC_PRESS_var;

public string FRAC_PRESS_var

{

get { return c_FRAC_PRESS_var;} set { c_FRAC_PRESS_var = value;}

}

public string PUMP_LUBE_PRESS = "PUMP LUBE PRESSURE";

private string c_PUMP_LUBE_PRESS_var;

public string PUMP_LUBE_PRESS_var

{

get { return c_PUMP_LUBE_PRESS_var;} set { c_PUMP_LUBE_PRESS_var = value;}

}

public string PUMP_LUBE_TEMP = "PUMP_LUBE TEMPERATURE";

private string c_PUMP_LUBE_TEMP_var;

public string PUMP_LUBE_TEMP_var

{

get { return c_PUMP_LUBE_TEMP_var;} set { c_PUMP_LUBE_TEMP_var = value;}

}

public string PUMP_FLOW = "PUMP FLOW RATE";

private string c_PUMP_FLOW_var;

public string PUMP_FLOW_var

{

get { return c_PUMP_FLOW_var;} set { c_PUMP_FLOW_var = value;}

}

public string ENG_HRS = "ENGINE HOURS";

private string c_ENG_HRS_var;

public string ENG_HRS_var

{

get { return c_ENG_HRS_var;} set { c_ENG_HRS_var = value;}

}

public string ENG_TYPE = "ENGINE TYPE";

private string c_ENG_TYPE_var;

public string ENG_TYPE_var

{

get { return c_ENG_TYPE_var;} set { c_ENG_TYPE_var = value;}

}

private string c_ENG_TYPE_name;

public string ENG_TYPE_name

{

get { return c_ENG_TYPE_name;} set { c_ENG_TYPE_name = value;}

}

public string TRANS_TYPE = "TRANSMISSION TYPE";

private string c_TRANS_TYPE_var;

public string TRANS_TYPE_var

{

get { return c_TRANS_TYPE_var;} set { c_TRANS_TYPE_var = value;}

}

private string c_TRANS_TYPE_name;

public string TRANS_TYPE_name

{

get { return c_TRANS_TYPE_name;} set { c_TRANS_TYPE_name = value;}

}

public string DIAG = "DIAGNOSTICS";

private string c_DIAG_var;

public string DIAG_var

{

get { return c_DIAG_var; } set { c_DIAG_var = value; }

}

}


It works now, but for better performance as far as memory management, aka. Garbage collection what's better?.


I've been reading on the class/struct thing, reference vs. value. I 'm leaning toward struct for performance and hopefully stability. I need the ability for each truck to have the ability to take and hold information. I could always figure away to pass the info to the monitor form


This below is what I've found:


  • You want your type to look and feel like a primitive type.

  • You create a lot of instances, use them briefly, and then drop them. For e.g., within a loop.

  • The instances you create are not passed around a lot.

  • You don't want to derive from other types or let others derive from your type.

  • You want others to operate on a copy of your data (basically pass by value semantics).

I'm I right to think to changing to a struct and if so what should the struct even look like? I hope this is enough detail.


One last question, is the class Engine complex enough to stay a class or can I safely change to a struct?


Thanks