If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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?

Re: Best Variable to Use

Originally Posted by VelvetTLeopard

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.

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.

Re: Best Variable to Use

Originally Posted by VelvetTLeopard

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.

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?".

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.

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.

Re: Best Variable to Use

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.

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).

Re: Best Variable to Use

Originally Posted by VelvetTLeopard

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.

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.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.