Hàm memoryview[] của Python trả về các đối tượng dạng xem bộ nhớ. Trước khi tìm hiểu thêm về hàm memoryview[], hãy xem tại sao chúng ta sử dụng hàm này
Tại sao chúng ta sử dụng chức năng memoryview[]?
Vì chế độ xem Bộ nhớ là một cách an toàn để hiển thị giao thức bộ đệm trong Python và chế độ xem bộ nhớ hoạt động giống như byte trong nhiều ngữ cảnh hữu ích [ví dụ: nó hỗ trợ giao thức ánh xạ] nên nó cung cấp sự thay thế thích hợp nếu được sử dụng cẩn thận. Điều tuyệt vời về nó là nó sử dụng giao thức bộ đệm bên dưới vỏ bọc để tránh các bản sao và chỉ sắp xếp các con trỏ tới dữ liệu. Vì vậy, trước khi tìm hiểu về chế độ xem bộ nhớ, trước tiên chúng ta cần hiểu Giao thức bộ đệm
Giao thức đệm
Giao thức bộ đệm cung cấp một cách để truy cập dữ liệu bên trong của một đối tượng. Dữ liệu nội bộ này là một mảng bộ nhớ hoặc bộ đệm. Nó cho phép một đối tượng hiển thị dữ liệu bên trong [bộ đệm] và đối tượng kia truy cập các bộ đệm đó mà không cần sao chép trung gian. Giao thức bộ đệm chỉ có thể truy cập được đối với chúng tôi ở cấp độ C-API và không sử dụng cơ sở mã thông thường của chúng tôi. Vì vậy, để hiển thị cùng một giao thức cho một cơ sở mã Python bình thường, các chế độ xem bộ nhớ sẽ có mặt
Chế độ xem bộ nhớ
các đối tượng memoryview cho phép mã Python truy cập dữ liệu bên trong của một đối tượng hỗ trợ giao thức bộ đệm mà không cần sao chép. Hàm memoryview[] cho phép truy cập đọc và ghi trực tiếp vào dữ liệu định hướng byte của đối tượng mà không cần sao chép dữ liệu đó trước. Điều đó có thể mang lại mức tăng hiệu suất lớn khi hoạt động trên các đối tượng lớn vì nó không tạo bản sao khi cắt
cú pháp. chế độ xem bộ nhớ [obj]
Thông số
- obj – đối tượng có dữ liệu bên trong sẽ được hiển thị
- hỗ trợ giao thức đệm - str và bytearray [nhưng không unicode]
Giá trị trả về. Trả về một đối tượng memoryview
Bộ nhớ Python[] Ví dụ
ví dụ 1. Bộ nhớ Python[] hoạt động
Python3
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']6
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']7
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']8
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']9
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']0
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']1
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']0
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']1
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']7
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']3
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']4
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']5
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']6
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']7
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']4
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']9
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']6
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']01
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']02
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']03
đầu ra
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']3
ví dụ 2. Sửa đổi dữ liệu nội bộ bằng memoryview
Python3
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']04
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']05
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']06
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']6
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']7
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']8
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']9
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']0
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']1
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']0
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']4
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']95____196
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']97
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']98
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']7
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']3
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']61
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']62______463____464
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']7
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']66
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']4
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']95____469
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']97
đầu ra.
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']
Giải thích cách chúng tôi sửa đổi dữ liệu nội bộ trong chương trình trên. Ở đây, chúng tôi đã cập nhật chỉ mục thứ 2 của chế độ xem bộ nhớ thành giá trị ASCII là 74 [J]. Trong đối tượng memoryview này, mem_view tham chiếu cùng bộ đệm hoặc bộ nhớ và cập nhật chỉ mục trong mem_view và nó cũng cập nhật byte_array
ví dụ 3. Bộ nhớ Python[] thành byte
Python3
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']71
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']06
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']6
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']7
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']8
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']9
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']0
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']1
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']0
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']98
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']7
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']3
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']4
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']95____485
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']86
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']87
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']7
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']89
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']4
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']95____485
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']93
đầu ra
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']0
Ví dụ 4. Bộ nhớ Python[] thành chuỗi
Python3
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']71
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']06
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']6
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']7
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']8
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']9
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']0
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']1
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']0
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']98
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']7
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']3
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']4
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']95____485
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']86
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']10
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']7
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']12____513
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']4
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']95____485
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']17
đầu ra
Before update: bytearray[b'XYZ'] After update: bytearray[b'XYJ']9
Tầm quan trọng của giao thức đệm và chế độ xem bộ nhớ
Bằng cách sử dụng giao thức bộ đệm, chúng tôi có thể làm việc trên dữ liệu lớn như chúng tôi muốn làm việc trên dữ liệu nhị phân của một hình ảnh. Giao thức bộ đệm, có thể tạo quyền truy cập đối tượng khác để sửa đổi dữ liệu lớn mà không cần sao chép nó. Điều này làm cho chương trình sử dụng ít bộ nhớ hơn và tăng tốc độ thực thi