One reason memoryviews are useful is that they can be sliced without copying the underlying data, unlike bytes/str.

For example, take the following toy example.

import time
for n in (100000, 200000, 300000, 400000):
    data = b'x'*n
    start = time.time()
    b = data
    while b:
        b = b[1:]
    print(f'     bytes {n} {time.time() - start:0.3f}')

for n in (100000, 200000, 300000, 400000):
    data = b'x'*n
    start = time.time()
    b = memoryview(data)
    while b:
        b = b[1:]
    print(f'memoryview {n} {time.time() - start:0.3f}')

On my computer, I get

     bytes 100000 0.211
     bytes 200000 0.826
     bytes 300000 1.953
     bytes 400000 3.514
memoryview 100000 0.021
memoryview 200000 0.052
memoryview 300000 0.043
memoryview 400000 0.077

You can clearly see the quadratic complexity of the repeated string slicing. Even with only 400000 iterations, it's already unmanageable. Meanwhile, the memoryview version has linear complexity and is lightning fast.

Edit: Note that this was done in CPython. There was a bug in Pypy up to 4.0.1 that caused memoryviews to have quadratic performance.

Answer from Antimony on Stack Overflow
🌐
Python
docs.python.org › 3 › c-api › memoryview.html
MemoryView objects — Python 3.15.0 documentation
A memoryview object exposes the C level buffer interface as a Python object which can then be passed around like any other object.
Top answer
1 of 6
133

One reason memoryviews are useful is that they can be sliced without copying the underlying data, unlike bytes/str.

For example, take the following toy example.

import time
for n in (100000, 200000, 300000, 400000):
    data = b'x'*n
    start = time.time()
    b = data
    while b:
        b = b[1:]
    print(f'     bytes {n} {time.time() - start:0.3f}')

for n in (100000, 200000, 300000, 400000):
    data = b'x'*n
    start = time.time()
    b = memoryview(data)
    while b:
        b = b[1:]
    print(f'memoryview {n} {time.time() - start:0.3f}')

On my computer, I get

     bytes 100000 0.211
     bytes 200000 0.826
     bytes 300000 1.953
     bytes 400000 3.514
memoryview 100000 0.021
memoryview 200000 0.052
memoryview 300000 0.043
memoryview 400000 0.077

You can clearly see the quadratic complexity of the repeated string slicing. Even with only 400000 iterations, it's already unmanageable. Meanwhile, the memoryview version has linear complexity and is lightning fast.

Edit: Note that this was done in CPython. There was a bug in Pypy up to 4.0.1 that caused memoryviews to have quadratic performance.

2 of 6
89

memoryview objects are great when you need subsets of binary data that only need to support indexing. Instead of having to take slices (and create new, potentially large, objects) to pass to another API, you can just take a memoryview object.

One such API example would be the struct module. Instead of passing in a slice of the large bytes object to parse out packed C values, you pass in a memoryview of just the region you need to extract values from.

memoryview objects, in fact, support struct unpacking natively; you can target a region of the underlying bytes object with a memoryview slice, then use .cast() to 'interpret' the underlying bytes as long integers, or floating point values, or n-dimensional lists of integers. This makes for very efficient binary file format interpretations, without having to create more copies of the bytes.

🌐
GeeksforGeeks
geeksforgeeks.org › python › memoryview-in-python
memoryview() in Python - GeeksforGeeks
January 12, 2026 - DSA Python · Data Science · NumPy · Pandas · Practice · Django · Flask · Last Updated : 12 Jan, 2026 · memoryview() provides direct access to an object’s memory (like bytes, bytearray, or array) without copying it, making operations on large datasets faster and more efficient.
🌐
W3Schools
w3schools.com › python › ref_func_memoryview.asp
Python memoryview() Function
The memoryview() function returns a memory view object from a specified object.
🌐
Real Python
realpython.com › ref › builtin-functions › memoryview
memoryview() | Python’s Built-in Functions – Real Python
The built-in memoryview() function provides a way to access the internal data of an object that supports the buffer protocol without copying it.
🌐
Chilkat
chilkatsoft.com › python_memoryview.asp
Explaining the Python memoryview
In Python, a "memoryview" is a built-in type that provides a zero-copy, efficient way to access and manipulate binary data stored in objects like "bytes", "bytearray", and other buffer-supporting objects.
🌐
Toppr
toppr.com › guides › python-guide › references › methods-and-functions › methods › built-in › memoryview › python-memoryview
Python memoryview() | What is Python memoryview() function? |
August 19, 2021 - The Python memoryview() function returns a memory view object based on the argument. Python memoryview() objects enable Python code to access the internal data of an object that implements the buffer protocol without the need for copying.
Find elsewhere
🌐
Medium
medium.com › @AlexanderObregon › how-pythons-memoryview-and-buffers-work-23c489fbbabb
How Python’s Memoryview and Buffers Work | Medium
August 11, 2024 - Both bytes and bytearray support ... Memoryview: As discussed earlier, memoryview objects provide a way to access the internal data of buffer-enabled objects without copying....
🌐
Codecademy
codecademy.com › docs › python › built-in functions › memoryview()
Python | Built-in Functions | memoryview() | Codecademy
August 26, 2023 - The memoryview() built-in function creates a memoryview object that allows Python code to access the internal data of an object’s buffer without making a copy of it.
🌐
Vultr Docs
docs.vultr.com › python › built-in › memoryview()
Python memoryview() - Create Memory View
September 27, 2024 - This code creates a memory view of the bytes_obj. The memoryview() function takes the bytes object as an argument and returns a view of its memory.
🌐
w3resource
w3resource.com › python › built-in-function › memoryview.php
Python memoryview() function - w3resource
August 19, 2022 - Python memoryview() function: The memoryview() function is used to get a memory view object from a specified object.
🌐
ArjanCodes
arjancodes.com › blog › using-memoryview-in-python-for-efficient-data-handling
Efficient Python Data Handling with MemoryView | ArjanCodes
May 16, 2024 - As you can see, memoryview, the buffer protocol and Buffer objects provide a powerful and Pythonic tool for interacting with low-level C buffer objects without the need to duplicate data.
🌐
CodeSteps
codesteps.com › home › python – memoryview – a quick overview with examples
Python – memoryview – A Quick Overview with Examples - CodeSteps
February 24, 2023 - Python - memoryview is another sequence type allows to access objects' (supports Buffer Protocol) internal memory; to deal with the data directly.
🌐
Medium
elshad-karimov.medium.com › pythons-memoryview-the-most-underappreciated-python-feature-acde51a8057f
Python’s memoryview – The Most Underappreciated Python Feature! 🚀 | by Elshad Karimov | Medium
February 8, 2025 - In Python, when you slice or modify ... and network applications. 🔹 Enter memoryview! A memoryview lets you work with binary data without making copies....
🌐
LabEx
labex.io › home › builtin › memoryview
Python memoryview() built-in function - Python Cheat Sheet
# Create a bytearray data = bytearray(b'hello world') # Create a memory view of the data view = memoryview(data) # Access the data through the view print(view[0]) # ASCII for 'h' print(view[6:11]) # a slice of the memory print(view[6:11].tobytes()) # You can also modify the underlying data through the view view[0] = 72 # ASCII for 'H' print(data) 104 <memory at 0x...> b'world' bytearray(b'Hello world') Blog: Python Data Types ·
🌐
Python Examples
pythonexamples.org › python-memoryview-builtin-function
memoryview() Built-in Function
Python memoryview() built-in function is used to get the memory view object that allows you to view the contents of a bytes-like object as a sequence of machine values. In this tutorial, you will learn the syntax and uses of memoryview() built-in function, and cover some examples.
🌐
Uwaterloo
icpc.student.cs.uwaterloo.ca › ~acm00 › python3-doc › c-api › memoryview.html
MemoryView objects — Python 3.10.12 documentation
Create a memoryview object to a contiguous chunk of memory (in either ‘C’ or ‘F’ortran order) from an object that defines the buffer interface. If memory is contiguous, the memoryview object points to the original memory.
🌐
Tutorialspoint
tutorialspoint.com › python › python_memoryview_function.htm
Python memoryview() Function
The Python memoryview() function is a built-in function used to obtain the "memory view" object from a specified object. This object allows us to access the internal data of any other object without copying the data.