[Tutorial] Data Type Usage
Author |
Message |
Mackie
|
Posted: Thu Apr 10, 2008 11:25 pm Post subject: [Tutorial] Data Type Usage |
|
|
Data Types
As requested, I'm going to go over some of the basics of data types that Turing offers. Now why do you care? Because if you want your programs to be efficient and work at top notch speed you need to know how to manage memory.
What is a data type?
A data type is template for an allocation of memory. Basically when you make a variable, that variable has a certain amount of the computers RAM, or memory, assigned to it. You can use no more, or less of that amount. Think of it like a bucket, a bucket will take up the same amount of space in your garage, no matter what you put inside of it.
Goal?
The whole point of this lesson is to teach you how to better use memory, to speed up your programs. Even though it's not much use in Turing, since Turing is already so slow, it will help in later programming endeavors I'm sure.
Bits and Bytes
Now, everybody knows what a bit and a byte are right? Well... Here it is anyway.
Bits stand for binary digits, basically just a fancy way of saying it is a switch set to 1 or 0.
A byte is 8 bits.
A kilobyte is 1024 bytes.
A megabyte is 1024 kilobytes.
A gigabyte is 1024 megabytes.
And so on... The less the better! Everything in the digital domain is a combination of 1's or 0's if you didn't know that already. It is how we measure memory usage, among other things.
The Basics
Data types are like onions, well no, actually they're not. They're more like data types actually. So most folks learn a few basic types to start out with.
Integer: Used to store positive of negative whole numbers.
Real/Double/Float: Used to store positive or negative numbers with decimals, it can also hold larger whole numbers than integers.
String: Used to store 'strings of characters' or simply just text.
Boolean: Used to store true of false values. (1 or 0)
The Details
Integers:
Again they are used to store whole numbers. They have a range of -2147483648 to 2147483647.
They are defined as such and use 4 bytes each.
How can we make these smaller Mackie!
Well Turing actually lets you specify how many, bits you want to be used per integer. You can declare then as any of the following: int1, int2, int4.
The number indicates how many byes are being used. Just like to mention that int4 is (almost) identical to int.
Boolean:
Booleans are always 1 byte. Why not a bit? I have no clue.
Turing: | var foobar : boolean |
Real:
Reals are also called, floating point numbers, or doubles. They are used to store any numbers between -10e308 and +10e308 with decimals of course.
They are defined as such and use 8 bytes each.
[syntax"turing"]var foobar : real [/syntax]
How can we make these smaller Mackie!
The alternatives to real include, real4 and real8. Reals should be avoided unless absolutely necessary. They are very costly to memory usage.
Natural:
Natural numbers range from 0 to 4294967294. They are can't hold decimals and are very useful when you don't have to go into the negatives
They are defined as such and use 4 bytes each.
How can we make these smaller Mackie!
The alternatives to nat include, nat1, nat2, and nat4. These can be very useful.
String:
Alright, strings are the most costly variable you'll run into. They take up 1 byte for every character allocated. By default 256 character(0 to 255) spaces are available. Thats 1 kilobyte for every 4 strings. :/. This needs to be taken down to size, but first off.
They are defined as such and use 256 bytes each.
Turing: | var foobar : string |
How can we make these smaller Mackie!
Hear is how you can set the size of your string!
Turing: | var foobar : string(30) |
You simply enter the number of characters you want in the brackets beside the data type. You can save on tons of space this way!
Constants:
Constants are nifty things. They only allocate as much space as they take up, so if you have the word "hello" it will only take up 5 bytes. If you have values that stay the same during the entire program you should try your hardest to keep them Constant.
Turing: | const FOOBAR := 50 |
Constants can hold any type of data.
There you go.
I hope you learned something. I sure did. Just remember that premature optimization is the root of all evil. Well.. That and MihaiG, but lets wait for the 4th comic for that one. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Nick
|
Posted: Thu Apr 10, 2008 11:34 pm Post subject: RE:[Tutorial] Data Type Usage |
|
|
nice tut I'd give you the applaud emote but I'm not sure what it is, I think it's ::applaud:: but I'm on the quick reply
+bits (not optimized of course )
EDIT: there's no clappy one? omg wtf I thought there was |
|
|
|
|
|
Carey
|
Posted: Thu Apr 10, 2008 11:40 pm Post subject: RE:[Tutorial] Data Type Usage |
|
|
Good tutorial to teach the basics of data types.
so ur a nighthawk too eh? |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Fri Apr 11, 2008 12:07 am Post subject: Re: [Tutorial] Data Type Usage |
|
|
+bits when I find out how to do it
Just a word on using less space and premature optimization:
Using types "less" than int could incur costs when conversions occur so that calculations can be done.
Think "signed extend" (on the processor).
Saving space by choosing the right type matters mostly when you have a large array or data structure.
For calculations, I will recommend int4 or nat4 (simply because they match the register size on most
(32-bit) systems).
I have seen (C++) programs that run faster with unsigned than with short. |
|
|
|
|
|
Clayton
|
Posted: Fri Apr 11, 2008 8:38 am Post subject: RE:[Tutorial] Data Type Usage |
|
|
You repeated multiple times that Mackie wrote: These can be very useful How about an example where this is true for each case? |
|
|
|
|
|
gitoxa
|
Posted: Fri Apr 11, 2008 1:32 pm Post subject: RE:[Tutorial] Data Type Usage |
|
|
That was quick. Bits for you
I will note though, constants don't take up any memory. When the program is compiled, it goes through and replaces all instances of the constant with the actual numbers/characters. |
|
|
|
|
|
Mackie
|
Posted: Fri Apr 11, 2008 1:49 pm Post subject: Re: RE:[Tutorial] Data Type Usage |
|
|
gitoxa @ Fri Apr 11, 2008 1:32 pm wrote: That was quick. Bits for you
I will note though, constants don't take up any memory. When the program is compiled, it goes through and replaces all instances of the constant with the actual numbers/characters.
Thanks for the bits, If I'm not mistaken I belive constants take up memory during interpretation atleast, didn't know about that compiler optimization though. |
|
|
|
|
|
Carey
|
Posted: Fri Apr 11, 2008 2:50 pm Post subject: RE:[Tutorial] Data Type Usage |
|
|
With an interpreter they take up space because the program reads the file one line at a time and has to remember what the constant is. With a compiler they don't take up any space, they are just used during compilation, not execution. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Mackie
|
Posted: Fri Apr 11, 2008 9:04 pm Post subject: Re: [Tutorial] Data Type Usage |
|
|
Some quick reference, there is also some numbers that I didn't show in the original.
P.S. I messed up formatting on the Real section example. Could a mod please fix that? Thanks, in advanced. |
|
|
|
|
|
Sean
|
Posted: Tue Apr 15, 2008 12:10 pm Post subject: RE:[Tutorial] Data Type Usage |
|
|
The chart I feel is more effective, and explains things more then just the standard way at the beginning.
Like the tutorial, keep it up Mackie! |
|
|
|
|
|
|
|