The Self-Taught Computer Scientist
Download 1.48 Mb. Pdf ko'rish
|
books-library.net-11301817Az7X6
- Bu sahifa navigatsiya:
- Computer memory
- Chapter 8
data structure has a fixed size, while a dynamic data structure can grow or shrink in size. Usually,
you define the size of a static data structure when you create it in your program. Once you make it, your data structure’s size is fixed and cannot change, but you can change the data’s values inside it. Python does not have static data structures; however, lower- level programming languages like C do. One problem with static data structures is that you must allocate a certain amount of memory for them. Computer memory is where your computer stores data. There are different kinds of memory, and explaining them all is outside the scope of this book, but for this chapter, you can think of memory as a place your computer stores data with an address it can use to reference it later. If the number of data elements you are working with ends up being smaller than the size you allocated, you’ve wasted memory space. Additionally, it may not be possible to add more elements than fit in the space you gave your data structure. Often the only way to add more elements to a static data structure is to allocate memory for a new structure big enough to contain the old and the new elements and then to copy the Chapter 8 What Is a Data Structure? 85 data from the old structure to this newly allocated memory together with the new elements. Because of this, static data structures are not the best data structure to use if you don’t know the number of elements you need to store in advance. However, if you know how many data elements you want to store and that number is not likely to change, then a static data structure often outperforms a dynamic one. For example, if you are maintaining a list of “undo” functions for a program that allows users up to 10 undo operations, a static data structure would be appropriate. Many data structures can be static or dynamic. For example, arrays (which we will discuss in the next chapter) are often static data structures, but many modern programming languages, such as Python, offer dynamic arrays (lists). In contrast to static data structures, you can easily change the size of a dynamic data structure. In a dynamic data structure, your computer allocates additional memory as you add more elements. When you remove elements, your computer frees that memory for other data. With their flexible size, dynamic data structures allow you to add and remove data elements efficiently, and these structures make efficient use of memory resources. However, accessing elements in dynamic data structures can be slow compared to static data structures, and storing a certain number of elements in dynamic data structures can often consume more memory than storing the same number of elements in static data structures. When dealing with an unknown amount of data, especially in situations where memory space is limited, dynamic data structures are often a good choice. Unless you are writing low- level code for an operating system or some other project where you have to squeeze out every possible performance optimization, you most likely will not spend a lot of time thinking about choosing a static or dynamic data structure. Instead, you will probably spend more time deciding whether you should use a linear or nonlinear data structure and, once you’ve made that decision, which linear or nonlinear data structure to use. As you learned earlier, different data structures have pros and cons. These pros and cons are mostly related to their efficiency in inserting, deleting, searching, and sorting data and how efficiently they use memory space. For example, it is incredibly efficient to check if an item is in a Python dictionary, even if it has a billion data pieces. It is not nearly as efficient to search a graph for a piece of data, though. In the next chapter, we’ll dive into more detail about when and how to use data structures. Vocabulary Download 1.48 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling