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);
}
相关推荐
喆星时瑜19 分钟前
Win10/11 安装 R 与 RStudio 详细图文教程
开发语言·r语言·安装
心中有你021435 分钟前
【路径规划】A*寻路算法最通俗易懂讲解(C语言完整实现+详细注释)
c语言·开发语言·算法
YYYing.39 分钟前
【设计模式系列 (七) 】桥接模式
c++·后端·设计模式·桥接模式
言乐61 小时前
Python自动去除水印
开发语言·python·django·virtualenv·pygame
大圣编蚕1 小时前
C语言入门:基本语法汇总
c语言·开发语言
czq_26867194871 小时前
Python打卡第31天
开发语言·python
invicinble1 小时前
python 编程语言 认识维度
开发语言·数据库·python
AC赳赳老秦1 小时前
数据采集全链路审计留痕:用 OpenClaw 实现合规审计与追溯
开发语言·汇编·python·php·swift·deepseek·openclaw
czhc11400756631 小时前
退出防呆:用“总闸“收口所有退出入口,以及三个并发语法坑
开发语言·c#
高山有多高2 小时前
【Linux笔记】Linux自定义Shell
linux·c++