Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. This is a performance issue: Struct vs. Class.

This is a performance issue: Struct vs. Class.

Scheduled Pinned Locked Moved C#
visual-studioperformancehelpquestionannouncement
7 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lecutus1
    wrote on last edited by
    #1

    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;

    D P L L L 5 Replies Last reply
    0
    • L Lecutus1

      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;

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      From your summry, it sems you've read this[^] article. If you read the section when not to use stucts you will see "should ideally be below 16 bytes". For that reason alone, with all those strings it's gonna be way above that, so stick with the class.

      Dave
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
      Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
      Why are you using VB6? Do you hate yourself? (Christian Graus)

      1 Reply Last reply
      0
      • L Lecutus1

        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;

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        I'd leave them as classes. Worrying about performance differences between class and struct is generally a waste of effort; "performance" is not a reason to choose one over the other. A truck is not a value, you don't perform operations with it.

        1 Reply Last reply
        0
        • L Lecutus1

          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;

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          If you are concerned about performance you shouldn't use strings for things such as IP addresses.. You can safely change it to a struct, but as you know it will pass by value, and it's big - so don't pass it. Just change "class" to "struct" and you're done. BUT, the struct vs class thing isn't likely to be the problem here (it occasionally pops up when dealing with several millions of instances of some type). I'd advice you to get a good profiler (such as ANTS from Red Gate) and look at what is really slowing it down.

          1 Reply Last reply
          0
          • L Lecutus1

            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;

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            1. class without a doubt, it really is an object, not a value; rather big; needs passing around; etc etc. As you have 20 odd string pairs in it, how will one more object make a difference? (suggestion: make the feature strings static) 2. your communication is using RS232C, which isn't really a fast interface. How can your code be in trouble performance-wise? How much is there to compute for one truck (250msec)? :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


            1 Reply Last reply
            0
            • L Lecutus1

              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;

              L Offline
              L Offline
              Lecutus1
              wrote on last edited by
              #6

              Sorry for wasting everbody's time. I'll do more research before asking anything. I thank everybody for there response. L.

              L 1 Reply Last reply
              0
              • L Lecutus1

                Sorry for wasting everbody's time. I'll do more research before asking anything. I thank everybody for there response. L.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                No problem. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups