Saturday, 16 September 2023

Data buffers in Python

Data buffer is a contiguous block of memory, used to store the data. Here the data can be of any type like integers, floating-point numbers, characters, or even more complex data structures like arrays or strings.

 

NumPy arrays internally use data buffers to perform the vector computations faster.

 

Advantages of Data buffers

a.   Faster data access: since the data buffer store the in contiguous memory locations, it is very efficient to access the data and perform numeric computations on it.

b.   Less memory overhead: Data buffers are homogeneous. That means all the elements of the data buffer are of same type. Because of this homogeneous nature, data buffers do not need to store the data type of each element.

 

data_buffer.py

import array

# Create an array of 32-bit integers
my_buffer = array.array('i', [1, 2, 3, 4, 5])

# Access elements in the buffer
element = my_buffer[2]
print('element at location 2 is : ', element)

# Modify an element in the buffer
my_buffer[1] = 99
print(my_buffer)  # Output: array('i', [1, 99, 3, 4, 5])

 


Output

element at location 2 is :  3
array('i', [1, 99, 3, 4, 5])

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment