What is data? Simply stated, data is information. From a programmer's perspective, however, the information contained in a piece of data can be stored in a computer many different ways. Given that you have choices about how to store data in a computer, how do you select the "correct" way to store a given piece of data in the computer? It depends.
As a starting point in making a decision about which data type to use for a specific task, you first need to know what the data type choices are. Table 4.1 presents a list of the common data types that are supported by Visual Basic .NET. The table also presents additional information about each data type to help you better understand the use for which each data type is best suited.
Table 4.1. Visual Basic .NET Basic Data Types
A quick look at Table 4.1 shows that there is a considerable range of values among the various data types. Sometimes the data type you need to use is obvious. For example, if you need to store the data value 1000 in a program, the Boolean and Byte data types will not work because they are not capable of storing the value 1000 . However, Char , Decimal , Double , and several other data types are quite capable of storing the value 1000 . How do you decide which of them to use?
Which Data Type to Use?
In all facets of life, decisions often involve tradeoffs. In terms of computer programming, the tradeoff often involves trading memory usage for speed or vice versa. In other cases, you have to trade memory usage for range. For example, if you use a Single data type, you use less memory for each number (4 bytes) than if you use a Double data type (8 bytes), but you are forced to accept a smaller range of values.
As a general rule, programmers are less concerned today about memory limitations than they are about the speed at which data is processed. Because the speed at which data is processed is very important, it seems to make sense that the smaller the number of bytes the program needs to manipulate while processing the data, the faster the program will run.
Well not really.
Data Type Selection and CPU Registers
When chipmakers design central processing unit (CPU) chips, they design them in such a way that certain parts of the chip, called registers, are most comfortable with data of a certain size. For example, most of the CPU chips that run the Windows operating system have registers that handle 4-byte data very efficiently . As a result, Integer data is a good choice in many situations, provided that the data falls within the range of an integer variable. In fact, in some instances, Integer data is processed faster than some of the data that uses fewer bytes simply because the smaller data types don't fit the "natural" size of the CPU's registers.
Let's reconsider our earlier decision about whether to use a Single or Double data type for very large numbers . The Single data type has fewer bytes, and if we don't need the extra range that a Double offers, it would seem that the Single data type should be a good choice. After all, it's only 4 bytes, just like an integer, and should fit naturally within the CPU registers, right?
Well, there's a little glitch in this thinking. The Single and Double data types are designed to represent floating-point numbers. Floating-point numbers are numeric values that may have fractional values, resulting in numbers that have decimal points in them. Because of this,floating-point numbers are not processed in the same way that integer numbers are. (Integer numbers are whole numbers and cannot have fractional values.)
Processing floating-point numbers is inherently slower than processing integer numbers. In fact, the processing is so much slower that chip manufacturers have recently begun to build small floating-point processors (FPPs) into CPU chips. (Prior to being part of the CPU itself, the FPP was a separate chip, like the 8087 by Intel.) The registers inside the FPPs are designed for 8-byte data values. Because of this, even though the Single data type uses fewer bytes than the Double type, it actually runs more slowly in most applications because it doesn't naturally fit the register size of the FFP. (This is because code must be executed to add an extra 4 bytes of empty data to the Single data type, and this operation takes time.) As a result, the Double data type is often the best choice for programs that crunch a lot of floating-point numbers.
Oh my you want to know the time, and I'm telling you how to build a watch. Still, the devil is in the details, and the more details you know, the better programmer you will be. Sometimes you can make better programming choices if you have a complete understanding about the hardware you're working with. As a friend of mine once said: "If you don't like these details, you should consider another line of work." There's a large chunk of truth there, so let's plow on.