Best Variable to Use
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: Best Variable to Use

Hybrid View

  1. #1
    Join Date
    Nov 2012
    Posts
    9

    Best Variable to Use

    Hello. I have a question that is more of a general programming question.

    I am making a class and I ran into a data member that I am having trouble deciding what variable type to use.

    I have a data that can only be one of two values. They are strings, say "black" and "White".

    I was thinking of just using the string type, but then I thought it might be better to use a bool since the value can take only one of two values.

    The data will be used to do so decision making as well as be displayed on the screen, possibly.

    What would be the optimum data type to use in this situation, because I can see the use of several?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Best Variable to Use

    Quote Originally Posted by VelvetTLeopard View Post
    I have a data that can only be one of two values. They are strings, say "black" and "White".

    I was thinking of just using the string type, but then I thought it might be better to use a bool since the value can take only one of two values.
    You can have both. This is just off the top of my head without putting a lot of thought into it:
    Code:
    #include <map>
    #include <string>
    #include <iostream>
    
    typedef std::map<bool, std::string> BoolMap;
    
    using namespace std;
    
    int main()
    {
       BoolMap myMap;
       // Initialize the map
       myMap.insert(make_pair(true,"White"));
       myMap.insert(make_pair(false,"Black"));
    
       // Prove it works
       cout << "The value of true is " << myMap[true] << "\n";
       cout << "The value of false is " << myMap[false];
    }
    I'm sure others will add alternatives to the above.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Nov 2012
    Posts
    9

    Re: Best Variable to Use

    That seems like a lot of unnecessary work. I am not trying to pair them. I just wanted to know what data type is best for a piece of data that can only two values. I thought since a bool is just a true or false, is it stored as a single bit or does it take an entire byte? Should I use a string and get it or an integer and convert to a string?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Best Variable to Use

    Quote Originally Posted by VelvetTLeopard View Post
    That seems like a lot of unnecessary work.
    I don't see any unnecessary work. The goal is to make the program understandable, easy to maintain, and unbreakable.

    In your original post, not only did you mention the variable type, but strings that are to be used with those types. By placing emphasis on not just the types, but what they will be used for in various parts of the program, you can encapsulate the entire concept in one type, i.e. a map. This is how you think in C++, and not low-level bits and bytes.

    By encapsulation, the concept of both the boolean and the string are kept in one place, instead of strewn all over in disjoint functions, types, etc. with no coherence. In one place, everyone knows what "true" means and what "false" means, instead of looking in various parts of the code and possibly making an error if they get the types reversed.
    Should I use a string and get it or an integer and convert to a string?
    And who will write this conversion function? Those two lines of code I posted does all of what you've just explained.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 22nd, 2012 at 11:31 PM.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Best Variable to Use

    Code:
    #include <map>
    #include <string>
    #include <iostream>
    
    typedef std::map<bool, std::string> BoolMap;
    
    using namespace std;
    
    bool BigFunction()
    {
       bool bReturnValue = false;
    
       // a lot of complex work not shown here
       //...
       //...
       return bReturnValue;
    }
    
    int main()
    {
       BoolMap myMap;
       // Initialize the map
       myMap.insert(make_pair(true,"White"));
       myMap.insert(make_pair(false,"Black"));
    
       cout << "The color I want is " << myMap[BigFunction()];
    }
    So the map is the "conversion function" you're looking for. The return value (whether it was true or false) was converted to the proper string name.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: Best Variable to Use

    Quote Originally Posted by VelvetTLeopard View Post
    I just wanted to know what data type is best for a piece of data that can only two values.
    It's to use a bool.

    Restriction is a very general principle of good programming. The purpose is to reduce complexity. The fewer the possibilities the fewer the things that can go wrong. Code becomes easier to read and comprehend. Compilers can make better optimization decisions.

    So declare types and variables in the narrowest scope possible. If it is a constant make it a constant. Limit outcomes to possible outcomes. Have just one entry and one exit point. Be brief. Don't waste resources. Etcetera.
    Last edited by nuzzle; November 23rd, 2012 at 01:31 AM.

  7. #7
    Join Date
    Nov 2012
    Posts
    9

    Re: Best Variable to Use

    Ok here is what I am trying to do. I have a class. In this class I want a data member that holds a value, one of two possible values, that will be used by the main program or other classes to determine other things. All I was wanting to know is what variable data type would be the best, as in most efficient, one to use. I never meant to say I had set a type and value. That was an example. I have not decided what type to use and those "strings" were only an example of one way this data COULD be represented. The whole issue was what data type is most efficient to use when the data can be only one of two possible values in general no matter what the data is. And I would write the code to convert from int to sting and vice versa.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Best Variable to Use

    Quote Originally Posted by VelvetTLeopard View Post
    Ok here is what I am trying to do. I have a class. In this class I want a data member that holds a value, one of two possible values, that will be used by the main program or other classes to determine other things. All I was wanting to know is what variable data type would be the best, as in most efficient, one to use.
    An integral type such as bool will always be more efficient than a string type, since each character of a string must be compared to know the final value.

    Even the example I showed you uses the bool to hold the integral type in the map. So my example not only answered your question, it shows how to associate that bool with strings without any need for conversion functions if you know the association between the bool and the string.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Nov 2012
    Posts
    9

    Re: Best Variable to Use

    So a simple bool will suffice. And once again I did not say the data WAS a string. I said the data COULD BE REPRESENTED as a string or bool or int, etc. The important fact was that the data type, what ever it was, would only have two possible values. I never asked for code in the first place. I know the basics of coding and always try to write my own code before asking for anything. Your example uses more than 13 lines of code BEFORE compilation, including an unnecessary #include in my opinion, that would result in a ton of lines just to store a piece of data that can be stored in a single bit. Plus, you never used any references, pointers, or inlining for such small functions and this would have dumbed down performance if I was looking at large numbers of interactions for this data member. And your example never answered my question because my question was "What data type is the most efficient one to hold data that can be only one of two values?" not "What data type or structure would I use to convert between an integer and a string depending on what the input was?".

  10. #10
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Best Variable to Use

    Your example uses more than 13 lines of code BEFORE compilation, including an unnecessary #include in my opinion, that would result in a ton of lines just to store a piece of data that can be stored in a single bit.
    What does an #include have to do with storage of data? So you write programs without #include files?

    The #include is for the map and for the cout. It is there to so that you or anyone else can copy and paste it into any compiler and run it without having to edit any lines.
    Plus, you never used any references, pointers, or inlining for such small functions
    What function? I just showed you that you can get the string value given the bool without writing any functions to convert back and forth between the bool and string. So you get the best of both worlds.

    Where does inlining and pointers come into play? As a matter of fact, it is good that no pointers are used -- this is the advantage of using high-level constructs, as pointers just become more maintenance.
    and this would have dumbed down performance if I was looking at large numbers of interactions for this data member.
    With C++, you cannot optimize by sight. This is the mistake that a lot of coders make, and that is judging C++ by what they see instead of actually compiling the code (with optimizations on) and running the program.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 23rd, 2012 at 12:22 AM.

  11. #11
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Best Variable to Use

    Another thing to consider is that if you have several true/false variables, then consider the std::bitset<> class instead of separate bools.

    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Nov 2012
    Posts
    9

    Re: Best Variable to Use

    I actually was thinking of a way to create a sort of "flag" word, but didn't know how to do it in C++. Assembly is no problem. I know what preprocessor directives are and I know how to use them. Do not assume I am new to programming. I am new to efficiency and performance. I just don't see why anyone would add an include, in this case #include<map>, to store a value because that single include file is going to add more lines than necessary. And you have 5 functions. You have a call to BigFunction(), two calls to insert(), and two calls to make_pair(). And as for pointers, I know they are becoming more outdated in lieu of references, but they can still be useful. You are passing variables by value in all five of the functions in your code. This means you are making copies of all the argument variables passed to them. Since you are merely displaying the data in these functions and not actually changing them, you really should pass them by reference, so they can not be changed. Plus, passing by reference does not make a copy of a variable's value, but creates a sort of pointer to that value instead. And as for judging optimization by sight, I can't do it yet, but people experienced can. All it takes is knowing the intricacies of thee variables and other parts of the code, how the lexicon and parser work, how the compiler works and a bit of Big O use. Yes, you can judge the "size" of a function by looking at it. You simply do the math of how often the function is called as well as what the functions does. Those two calls to make_pair() are creating in memory four more variable values plus the two they used to copy. That means your code snippet has allocated memory for six variables and that is for each time the snippet runs.

  13. #13
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Best Variable to Use

    Quote Originally Posted by VelvetTLeopard View Post
    I just don't see why anyone would add an include, in this case #include<map>, to store a value because that single include file is going to add more lines than necessary.
    Are you going to attempt to write a C++ program without using the standard library, which means you must #include files?
    You have a call to BigFunction()
    Which I am using just as an example
    two calls to insert(), and two calls to make_pair().
    Those are done to initialize the map. Once the map is initialized, there is no need for further initialization. The new version of C++ has nicer initialization syntax -- would that have changed your take on the code if I used C++ 11? What you're concentrating on is moot, as again, it is there to initialize the map, nothing else.
    You are passing variables by value in all five of the functions in your code.
    Where are variables passed by value? Can you point out where values are passed by value?
    This means you are making copies of all the argument variables passed to them. Since you are merely displaying the data in these functions and not actually changing them, you really should pass them by reference, so they can not be changed.
    I wish I knew what you are referring to.
    And as for judging optimization by sight, I can't do it yet, but people experienced can.
    I guess 20+ years of doing C++ isn't experience enough. OK.

    The code I posted is a sample of how a map operates. Nothing more, nothing less. It's there so you can see the syntax and the higher-level constructs that can be used to make the program maintainable.
    All it takes is knowing the intricacies of thee variables and other parts of the code, how the lexicon and parser work, how the compiler works and a bit of Big O use.
    You are talking about bare algorithms. I am talking about looking at C++ higher extractions and classes such as map, vector, bitset, functors, etc. and attempting to judge "Big O" based on that information. It has been proven time and time again that you cannot take C++ code using these constructs and firmly state what is fast or slow unless you run the program and time it.
    Yes, you can judge the "size" of a function by looking at it. You simply do the math of how often the function is called as well as what the functions does. Those two calls to make_pair() are creating in memory four more variable values plus the two they used to copy. That means your code snippet has allocated memory for six variables and that is for each time the snippet runs.
    OK, what about this:
    Code:
    int main()
    {
       BoolMap myMap;
       // Initialize the map
       myMap[true] = "White";
       myMap[false] = "Black";
    }
    The question -- is this faster than using make_pair()?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 23rd, 2012 at 01:11 AM.

  14. #14
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Best Variable to Use

    Using bitset:
    Code:
    #include <bitset>
    
    int main()
    {
        std::bitset<8> myBitSet;  // this makes room for 8 on/off values.
        myBitSet.set(0, false);  // sets bit 0 to false
        // or
        myBitSet[0] = false;  // sets bit 0 to false
    }
    The bitset class takes less space on the whole, since it is optimized to use a bit instead of an entire bool as a "true/false" value. So for example, something like sizeof(bitset<16>) is more than likely smaller than sizeof(bool) * 16.

    Again, look at the set() function, and look at the alternate syntax using []. Which is faster? Or does it matter? Isn't it a case as to which one is readable or understandable to you or your fellow programmers, and not a matter of speed (and to be honest, there is not a question on speed, since both methods are comparable).

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 23rd, 2012 at 01:29 AM.

  15. #15
    Join Date
    Nov 2012
    Posts
    9

    Re: Best Variable to Use

    bitset seems the way to go then, but I would want to come up with more flags for the program, which shouldn't be hard.

    For performance, it may matter or it may not, but the class this is part of will be instantiated for many, many, and maybe many, many more agents in the system, all of which may test this value for each and every other instance of the class so if there are, say, 100 agents in the program, each one would check this value of each other instance, giving you 9,900 checks for the value, and this class was designed to check this value whenever another class satisfies the check's requirements, say time of day, and if all instances are online at the same time, worst case scenario, 9,900 checks. Now assume this check is part of an overhead loop for the program and each class makes this check every second. Now this value check is happening, again with a worst case scenario, 9,900 times per second, or 594,000 times per minute. That is why I want the most efficient data type.

    I will look into bitset, but I would like to ask more question on the subject: Would inline assembly for C++ help me with manipulating single bits better than C++ alone does? I already know assembly and enjoy it.

    nuzzle posted before I got this post done. That makes a lot of sense. I am ok with using a bool because the data will never be interpreted in a way the human will see except for possibly one or two instances, where a simple conversion can be made.
    Last edited by VelvetTLeopard; November 23rd, 2012 at 01:41 AM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center