Zero-Copy Data Processing in Python with memoryview

# Create a bytearray with some data
data = bytearray(b”Hello, World!”)

# Create a memoryview on the bytearray
view = memoryview(data)

# Access the data directly using the memoryview (no copying)
print(“Original data:”, view.tobytes()) # Print original data as bytes

# Modify the data through the memoryview
view[7:12] = b”Python”

# Check how the changes are reflected in the original data
print(“Modified data:”, data)

# Show that no copying occurred
print(f”Memoryview object: {view}”)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top