C++点云大文件读取

C++点云大文件读取

  • [1. 常规读取](#1. 常规读取)
    • [1.1 逐行读取](#1.1 逐行读取)
    • [1.2 逐字节读取](#1.2 逐字节读取)
  • [2. 并行读取 (Multithreading)](#2. 并行读取 (Multithreading))
  • [3. 使用缓冲读取 (Buffered I/O)](#3. 使用缓冲读取 (Buffered I/O))
  • [4. 内存映射文件 (Memory Mapping)](#4. 内存映射文件 (Memory Mapping))

在C++中读取大文件时,如果需要提高读取速度,可以考虑以下几种方法:

1. 常规读取

常规读取是指直接使用 std::ifstream 或 std::getline 逐个字符或逐行读取文件。适用于小文件或需要逐行处理内容的场景。

1.1 逐行读取

cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>

void read_large_file(const std::string& filename) {
    std::ifstream file(filename);
    if (!file) {
        std::cerr << "Failed to open file" << std::endl;
        return;
    }

    std::string line;
    while (std::getline(file, line)) {
        // 处理读取的行
    }

    file.close();
}

1.2 逐字节读取

cpp 复制代码
#include <iostream>
#include <fstream>

void read_large_file(const std::string& filename) {
    std::ifstream file(filename, std::ios::binary);
    if (!file) {
        std::cerr << "Failed to open file" << std::endl;
        return;
    }

    char byte;
    while (file.get(byte)) {
        // 处理读取的字节
    }

    file.close();
}

2. 并行读取 (Multithreading)

对于大文件,如果处理的内容可以并行化(例如按行、按块处理),可以使用多线程来加速读取过程。在多核处理器上,这种方法能够显著提升文件读取的性能。可以使用标准库的 std::thread 或者并行框架如 OpenMP、TBB 等来实现。

cpp 复制代码
复制代码
#include <iostream>
#include <fstream>
#include <vector>
#include <thread>

void process_chunk(const std::vector<char>& buffer, size_t start_idx, size_t end_idx) {
    // 处理 buffer[start_idx, end_idx] 范围内的数据
}

void read_large_file(const std::string& filename) {
    std::ifstream file(filename, std::ios::binary);
    if (!file) {
        std::cerr << "Failed to open file" << std::endl;
        return;
    }

    file.seekg(0, std::ios::end);
    size_t file_size = file.tellg();
    file.seekg(0, std::ios::beg);

    size_t chunk_size = file_size / 4;  // 假设使用 4 个线程
    std::vector<std::thread> threads;

    for (size_t i = 0; i < 4; ++i) {
        size_t start_idx = i * chunk_size;
        size_t end_idx = (i + 1) * chunk_size;
        if (i == 3) end_idx = file_size;

        std::vector<char> buffer(chunk_size);
        file.seekg(start_idx, std::ios::beg);
        file.read(buffer.data(), chunk_size);

        threads.push_back(std::thread(process_chunk, std::ref(buffer), start_idx, end_idx));
    }

    for (auto& t : threads) {
        t.join();
    }

    file.close();
}

3. 使用缓冲读取 (Buffered I/O)

使用缓冲区可以减少频繁的磁盘 I/O 操作,从而加速文件读取过程。C++ 标准库中的 std::ifstream 默认使用缓冲读取。你可以通过调整缓冲区的大小来优化性能。比如,可以使用 std::ifstream 配合 std::vector 来增加读取块的大小。

  • 缓冲区大小的经验值
    对于较小的文件(几 MB),可以选择几 KB 到几十 KB 的缓冲区。
    对于较大的文件(几十 MB 到几 GB),可以选择较大的缓冲区(几百 KB 到 1MB)。
  • 内存的限制
    缓冲区大小也需要考虑系统的内存使用情况。对于非常大的文件(如 10 GB 或更大),如果缓冲区太大,可能会导致内存不足,从而影响系统的性能。根据系统内存选择合理的大小。

示例代码

cpp 复制代码
#include <iostream>
#include <fstream>
#include <vector>

void read_large_file(const std::string& filename) {
    std::ifstream file(filename, std::ios::binary);
    if (!file) {
        std::cerr << "Failed to open file" << std::endl;
        return;
    }

    // 设置一个合适大小的缓冲区,比如 1MB
    std::vector<char> buffer(1024 * 1024);

    while (file.read(buffer.data(), buffer.size())) {
        // 处理读取的数据
        // 可以根据需求处理 buffer
    }

    // 读取剩余的部分
    if (file.gcount() > 0) {
        // 处理剩余的数据
        // 可以根据需求处理 buffer 中的数据
    }

    file.close();
}

4. 内存映射文件 (Memory Mapping)

内存映射文件通过将文件的内容映射到进程的内存空间,可以避免频繁的 I/O 操作,提高文件读取的效率。在大文件处理时,内存映射尤其有用。

  • 缺点
    对于大于10G的文件,当内存不足时会导致读取不全的问题
    该方法不支持长路径,不支持中文路径(本人测试了很多方法,均不成功)
cpp 复制代码
#include <iostream>
#include <fstream>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>

void read_large_file(const std::string& filename) {
    int fd = open(filename.c_str(), O_RDONLY);
    if (fd == -1) {
        std::cerr << "Failed to open file" << std::endl;
        return;
    }

    off_t file_size = lseek(fd, 0, SEEK_END);
    lseek(fd, 0, SEEK_SET);

    void* mapped_mem = mmap(nullptr, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
    if (mapped_mem == MAP_FAILED) {
        std::cerr << "Failed to map file to memory" << std::endl;
        close(fd);
        return;
    }

    // 使用映射的内存读取文件内容
    char* file_data = static_cast<char*>(mapped_mem);
    // 可以直接处理 file_data

    // 完成后解除映射
    munmap(mapped_mem, file_size);
    close(fd);
}
相关推荐
晚雾也有归处22 分钟前
链表(C++)
数据结构·c++·链表
-代号952723 分钟前
【JavaScript】十二、定时器
开发语言·javascript·ecmascript
勘察加熊人27 分钟前
c++实现录音系统
开发语言·c++
self-discipline63432 分钟前
【Java】Java核心知识点与相应面试技巧(七)——类与对象(二)
java·开发语言·面试
wei38724523236 分钟前
java笔记02
java·开发语言·笔记
李余博睿(新疆)1 小时前
青少年软件编程(C语言)等级考试试卷(三级)
c++
CANI_PLUS1 小时前
python 列表-元组-集合-字典
开发语言·python
老秦包你会1 小时前
QT第六课------QT界面优化------QSS
开发语言·qt
難釋懷1 小时前
JavaScript基础-history 对象
开发语言·前端·javascript
东方佑2 小时前
使用 Python 自动处理 Excel 数据缺失值的完整指南
开发语言·python·excel