What is a dynamic array?
A dynamic array is similar to an array, but with the difference that its size can be dynamically modified at runtime. Donβt need to specify how much large an array beforehand. The elements of an array occupy a contiguous block of memory, and once created, its size cannot be changed. A dynamic array can, once the array is filled, allocate a bigger chunk of memory, copy the contents from the original array to this new space, and continue to fill the available slots.
#data_structure
@Cmatrix_PY
A dynamic array is similar to an array, but with the difference that its size can be dynamically modified at runtime. Donβt need to specify how much large an array beforehand. The elements of an array occupy a contiguous block of memory, and once created, its size cannot be changed. A dynamic array can, once the array is filled, allocate a bigger chunk of memory, copy the contents from the original array to this new space, and continue to fill the available slots.
#data_structure
@Cmatrix_PY
π1
Dynamic Array Logic Implementation:
The key is to provide means to grows an array A that stores the elements of a list. We canβt actually grow the array, its capacity is fixed. If an element is appended to a list at a time, when the underlying array is full, we need to perform following steps.
1.Allocate a new array B with larger capacity (A commonly used rule for the new array is to have twice the capacity of the existing array )
2.Set B[i]=A[i], for i=0 to n-1 where n denotes the current no of items.
3.Set A=B that is, we hence forth use B as the array of supporting list.
4.Insert new element in the new array.
#data_structure
@Cmatrix_PY
The key is to provide means to grows an array A that stores the elements of a list. We canβt actually grow the array, its capacity is fixed. If an element is appended to a list at a time, when the underlying array is full, we need to perform following steps.
1.Allocate a new array B with larger capacity (A commonly used rule for the new array is to have twice the capacity of the existing array )
2.Set B[i]=A[i], for i=0 to n-1 where n denotes the current no of items.
3.Set A=B that is, we hence forth use B as the array of supporting list.
4.Insert new element in the new array.
#data_structure
@Cmatrix_PY
π1
Why tuple Load faster than list?
Tuples can load faster than lists when they contain constants (such as integers and strings) because tuples are immutable, meaning their values cannot be changed once they are created. This immutability allows the interpreter to make certain optimizations during the loading process.
When a tuple is created with constant values, the compiler or interpreter can treat it as a single constant value in memory. This means that the entire tuple can be loaded in one step without any intermediate steps or memory allocations. Since tuples are immutable, there is no need for additional memory allocation or resizing because the values cannot be modified later.
On the other hand, lists are mutable, so they need to support dynamic resizing and element addition. When building a list, each element is added individually, which involves multiple steps of memory allocation, resizing, and assignment. The list needs to allocate memory for each new element, potentially resize the underlying data structure, and copy the existing elements to the new memory location.
Because of these differences, tuples can load faster than lists when they contain constant values because the loading process for tuples is more streamlined and doesn't require the same level of memory management and resizing operations performed by lists.
It's important to note that this performance difference between tuples and lists is generally only noticeable in specific scenarios where large collections or intensive operations are involved. For most general use cases, the speed difference between tuples and lists is unlikely to have a significant impact on overall performance.
#data_structure
#python
@Cmatrix_PY
Tuples can load faster than lists when they contain constants (such as integers and strings) because tuples are immutable, meaning their values cannot be changed once they are created. This immutability allows the interpreter to make certain optimizations during the loading process.
When a tuple is created with constant values, the compiler or interpreter can treat it as a single constant value in memory. This means that the entire tuple can be loaded in one step without any intermediate steps or memory allocations. Since tuples are immutable, there is no need for additional memory allocation or resizing because the values cannot be modified later.
On the other hand, lists are mutable, so they need to support dynamic resizing and element addition. When building a list, each element is added individually, which involves multiple steps of memory allocation, resizing, and assignment. The list needs to allocate memory for each new element, potentially resize the underlying data structure, and copy the existing elements to the new memory location.
Because of these differences, tuples can load faster than lists when they contain constant values because the loading process for tuples is more streamlined and doesn't require the same level of memory management and resizing operations performed by lists.
It's important to note that this performance difference between tuples and lists is generally only noticeable in specific scenarios where large collections or intensive operations are involved. For most general use cases, the speed difference between tuples and lists is unlikely to have a significant impact on overall performance.
#data_structure
#python
@Cmatrix_PY
π2π₯1