What costs more space: a structure or switch statement?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dissectcode2
    New Member
    • Apr 2009
    • 32

    What costs more space: a structure or switch statement?

    What takes up more space: a structure or switch statement, with the same amount of members? I am assuming the space used is determined by the compiler; mine is gcc.

    thanks!
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    ? I think you're comparing apples to oranges.

    the structure, since it needs to know what type and name of each of its members in addition to the storage space. A switch has all the same type of members with no names.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      A structure is a data type that specifies the contents of a variable; a switch statement is executable code. I don't know how to compare them.

      Please provide a short example of what you envision to be an equivalent structure and switch statement. I think that will help us answer you better.

      Comment

      • dissectcode2
        New Member
        • Apr 2009
        • 32

        #4
        Originally posted by donbock
        A structure is a data type that specifies the contents of a variable; a switch statement is executable code. I don't know how to compare them.

        Please provide a short example of what you envision to be an equivalent structure and switch statement. I think that will help us answer you better.
        Code:
        typedef struct s_nameTypes 
        {
          int    id;
          char *name;
        } NameTypes;
        
        int apples = 0x1;
        int oranges = 0x2;
        
        NameTypes names[] =
        {
          { apples, "i am an apple" },
          { oranges, "i am an orange" },
          { 0x0, "i don't know what i am" }
        }
        
        while( names[i] != 0x0 )
          if( someID == names[i].id )
             strcpy(myname, names[i].name);
        vs. a switch statement whose cases are the IDs:

        Code:
        int apples = 0x1;
        int oranges = 0x2;
        
        switch( someID )
        {
          case oranges :
              strcpy( myname, "I am an apple!");
              break;
        
          case apples :
              strcpy( myname, "I am an orange!");
               break;
        }

        So, which one is better?

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          So you essentially have a database; id is the key field and name is an attribute field. Given an id value, you want to extract an attribute field from the corresponding database record. You're asking if it is better to have an array that explicitly models the database, or construct the database implicitly within your executable code.

          In my opinion the most important measures of which approach is better are the following. The amount of space consumed is not very important.
          • easiest to understand
          • easiest to implement
          • easiest to maintain
          • most robust (hardest to break)

          In my opinion an explicit database wins in every category.

          By the way, I don't think the code you provided will work; but that doesn't change my conclusion.

          Comment

          • dissectcode2
            New Member
            • Apr 2009
            • 32

            #6
            Originally posted by donbock
            In my opinion the most important measures of which approach is better are the following. The amount of space consumed is not very important.
            .
            If the database contains a million entries then the space consumption is relatively important. Hence my question.

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              It is hard to believe you are contemplating a switch statement with a million cases.

              Do you know ahead of time if any of those million records definitely won't need to be searched? Do you know ahead of time if any of the fields won't be used at all? If you can't rule out any of the records or fields then they will all be there one way or another, whether it is in an array or distributed throughout your code.

              Execution time may be more important that space. Can you come up with a search algorithm that will find the matching record faster?

              Comment

              • dissectcode2
                New Member
                • Apr 2009
                • 32

                #8
                Originally posted by donbock
                It is hard to believe you are contemplating a switch statement with a million cases.

                Do you know ahead of time if any of those million records definitely won't need to be searched? Do you know ahead of time if any of the fields won't be used at all? If you can't rule out any of the records or fields then they will all be there one way or another, whether it is in an array or distributed throughout your code.

                Execution time may be more important that space. Can you come up with a search algorithm that will find the matching record faster?
                Would you suggest using a hash table? Please share your experteese...

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  The most important thing is to not retype any of this million-record database. What form does it take (text file, binary file, initializer in a source file, etc.)? How many times do you have to search the database?

                  Comment

                  Working...