Page 164 -
P. 164

data in files and arrays


           Sorting is easier in memory


           If you are writing a program that is going to deal with a lot of data, you
           need to decide where you need to keep that data while the program works
           with it. Most of the time, you will have two choices:

               1     Keep the data in files on the disk.
                     If you have a very large amount of data, the obvious place to put it is on
                     disk. Computers can store far more data on disk than they can in memory.
                     Disk storage is persistent: if you yank the power cord, the computer
                     doesn’t forget the information written on the disk. But there is one real
                     problem with manipulating data on disk: it can be very slow.

               2     Keep the data in memory.
                     Data is much quicker to access and change if it’s stored in the computer’s
                     memory. But, it’s not persistent: data in memory disappears when your
                     program exits, or when the computer is switched off (unless you remember
                     to save it to a file, in which case it becomes persistent).


           Keep the data in memory
                                                                                       You have lots of lines of
           If you want to sort a lot of data, you will need to shuffle data around     data, so you'll need lots of
           quite a lot. This is much faster in memory than on disk.                    variables... right?!?
           Of course, before you sort the data, you need to read it into memory,
           perhaps into a large number of individual variables:

          You need to move
          the lines of data
          in the results.txt
          file into memory
           before trying to
           sort it.








                                                                                         Do you have a big
                                                                                          cupboard?!? Because that's
                 You are going to have a problem if you attempt to move all those         a lot of jars...
                  lines of data into the computer‛s memory. What type of problem do
                  you think you‛ll have?



                                                                                       you are here 4    129
   159   160   161   162   163   164   165   166   167   168   169