Files
ailearning/docs/da/050.md
2020-10-19 21:08:55 +08:00

33 lines
1.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 内存映射
**Numpy** 有对内存映射的支持。
内存映射也是一种处理文件的方法,主要的函数有:
* `memmap`
* `frombuffer`
* `ndarray constructor`
内存映射文件与虚拟内存有些类似,通过内存映射文件可以保留一个地址空间的区域,同时将物理存储器提交给此区域,内存文件映射的物理存储器来自一个已经存在于磁盘上的文件,而且在对该文件进行操作之前必须首先对文件进行映射。
使用内存映射文件处理存储于磁盘上的文件时将不必再对文件执行I/O操作使得内存映射文件在处理大数据量的文件时能起到相当重要的作用。
## memmap
```py
memmap(filename,
dtype=uint8,
mode='r+'
offset=0
shape=None
order=0)
```
`mode` 表示文件被打开的类型:
* `r` 只读
* `c` 复制+写,但是不改变源文件
* `r+` 读写,使用 `flush` 方法会将更改的内容写入文件
* `w+` 写,如果存在则将数据覆盖
`offset` 表示从第几个位置开始。