测试C++中三种写文件方法的速度

原帖https://stackoverflow.com/questions/11563963/writing-a-binary-file-in-c-very-fast,笔者在此基础上将原来的option3替换为unistd.h中的write,因为原来option3和option1差不多,且测试结果没有什么区别。

方法1:fstream中到write
方法2:C中的fwrite
方法3:unistd.h中的write

#include <fstream>
#include <chrono>
#include <vector>
#include <cstdint>
#include <numeric>
#include <random>
#include <algorithm>
#include <iostream>
#include <cassert>
#include <unistd.h>
#include <fcntl.h>


std::vector<uint64_t> data;

std::vector<uint64_t> GenerateData(std::size_t bytes)
{
    assert(bytes % sizeof(uint64_t) == 0);
    std::vector<uint64_t> data(bytes / sizeof(uint64_t));
    std::iota(data.begin(), data.end(), 0);
    std::shuffle(data.begin(), data.end(), std::mt19937{ std::random_device{}() });
    return data;
}

long long option_1(std::size_t bytes)
{
    auto startTime = std::chrono::high_resolution_clock::now();
    auto myfile = std::fstream("file.binary", std::ios::out | std::ios::binary);
    myfile.write((char*)&data[0], bytes);
    myfile.close();
    auto endTime = std::chrono::high_resolution_clock::now();

    return std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
}

long long option_2(std::size_t bytes)
{
    auto startTime = std::chrono::high_resolution_clock::now();
    FILE* file = fopen("file.binary", "wb");
    fwrite(&data[0], 1, bytes, file);
    fclose(file);
    auto endTime = std::chrono::high_resolution_clock::now();

    return std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
}

long long option_3(std::size_t bytes)
{
    auto startTime = std::chrono::high_resolution_clock::now();
    int file_fd = open("file.binary", O_WRONLY | O_CREAT, 00666);
    write(file_fd, &data[0], bytes);
    close(file_fd);
    auto endTime = std::chrono::high_resolution_clock::now();

    return std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
}

int main(int argc, char * argv[])
{
	if(argc < 2)
	{
		std::cout << "Need argv[1],like 100 (Unit:MB)" << std::endl;	
		return 0;
	}

    const std::size_t kB = 1024;
    const std::size_t MB = 1024 * kB;
    const std::size_t GB = 1024 * MB;

	size_t size = atoi(argv[1]) *MB;	
	
	data = GenerateData(size);
	
	std::cout << "Testing begin...."  << std::endl;
	
    std::cout << "option1, " << size / MB << "MB: " << option_1(size) << "ms" << std::endl;
    std::cout << "option2, " << size / MB << "MB: " << option_2(size) << "ms" << std::endl;
    std::cout << "option3, " << size / MB << "MB: " << option_3(size) << "ms" << std::endl;
	
	unlink("file.binary");
	
    return 0;
}

测试环境:Ubuntu 16.04
测试结果:

  1. $ ./Test_write_speed 50
    Testing begin…
    option1, 50MB: 129ms
    option2, 50MB: 129ms
    option3, 50MB: 98ms
  2. $ ./Test_write_speed 100
    Testing begin…
    option1, 100MB: 249ms
    option2, 100MB: 257ms
    option3, 100MB: 196ms
  3. $ ./Test_write_speed 300
    Testing begin…
    option1, 300MB: 731ms
    option2, 300MB: 763ms
    option3, 300MB: 583ms
  4. $ ./Test_write_speed 500
    Testing begin…
    option1, 500MB: 1239ms
    option2, 500MB: 1271ms
    option3, 500MB: 968ms

结论:
unistd.h中的write最快

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的脚本示例,用于测试 C++ 的 VideoCapture 的速度: ```cpp #include <opencv2/opencv.hpp> #include <iostream> #include <chrono> int main() { // 打开视频文件 cv::VideoCapture cap("your_video_file.mp4"); if (!cap.isOpened()) { std::cerr << "无法打开视频文件" << std::endl; return -1; } // 获取视频的帧率和总帧数 double fps = cap.get(cv::CAP_PROP_FPS); int totalFrames = cap.get(cv::CAP_PROP_FRAME_COUNT); // 定义计时器 std::chrono::steady_clock::time_point start, end; // 循环读取并处理每一帧 cv::Mat frame; int frameCount = 0; while (cap.read(frame)) { if (frame.empty()) { std::cerr << "无法读取帧" << std::endl; break; } // 处理帧... frameCount++; // 输出当前帧数 std::cout << "当前帧数: " << frameCount << std::endl; // 判断是否到达总帧数,如果是则跳出循环 if (frameCount >= totalFrames) { break; } } // 计算时间差并输出平均帧率 double elapsedTime = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() / 1000.0; double avgFps = frameCount / elapsedTime; std::cout << "平均帧率: " << avgFps << std::endl; return 0; } ``` 请将 `your_video_file.mp4` 替换为您要测试的视频文件的路径。 在这个脚本,我们使用 OpenCV 的 VideoCapture 类来打开视频文件。然后,我们使用循环来逐帧读取视频,并在每一帧上进行处理(您可以根据实际需求进行修改)。我们还记录当前帧数,并在每一帧上输出当前帧数。 脚本结束后,我们计算从开始到结束的时间差,并根据总帧数计算平均帧率。这样可以测试 VideoCapture 的读取速度。 请注意,此脚本仅用于测试 VideoCapture 的速度,您可以根据实际需求进行修改和扩展。另外,确保已经正确安装并配置了 OpenCV C++ 库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值