Linux 下,读取bin格式的RGB图片或是 yuv_nv12 图片的 C++程序并测接口耗时

该代码示例展示了使用C++进行RGB和NV12格式图像的二进制读写函数,并通过ftime获取函数执行的毫秒级时间消耗,用于性能评估。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实测好用,可以直接给出 ms 级别的时间

 #include<iostream>
 #include<stdio.h>
 #include<sys/timeb.h>

void read_rgb_from_bin(std::string file_path, int w, int h, float * im){
    FILE *fp = fopen(file_path.c_str(), "rb");
    int size = (int)(w * h * 3);
    fwrite(im, 1, size, fp);
    fclose(fp);    
}

void write_rgb_to_bin(std::string file_path, int w, int h, float * im){
    FILE *fp = fopen(file_path.c_str(), "wb");
    int size = (int)(w * h * 3);
    fwrite(im, 1, size, fp);
    fclose(fp);    
}

void read_nv12_from_bin(std::string file_path, int w, int h, float * im){
    FILE *fp = fopen(file_path.c_str(), "rb");
    int size = (int)(w * h * 3 / 2.0);
    fread(im, 1, size, fp);
    fclose(fp);    
}

void write_nv12_to_bin(std::string file_path, int w, int h, float * im){
    FILE *fp = fopen(file_path.c_str(), "wb");
    int size = (int)(w * h * 3 / 2.0);
    fwrite(im, 1, size, fp);
    fclose(fp);    
}

int main()
 {
 std::string pic_in_path = "/home/luxy/pic_in/3840_2160_rgb.bin";
   int pic_in_width = 3840;
   int pic_in_height = 2160;
   float *pic_out = new float[3840*2160*3];
   struct timeb api_start;
   struct timeb api_end;
   long cost_time ;
   /* 获取当前时间 */
   ftime(&api_start);  
   read_rgb_from_bin(pic_in_path, pic_in_width, pic_in_height, pic_out);
   /* 获取当前时间 */
   ftime(&api_end); 
   cost_time =(api_end.time-api_start.time)*1000+(api_end.millitm-api_start.millitm); /* 计算毫秒级的时间 */
   std::cout << "API consumption is " << cost_time << "ms" << std::endl;
   delete[] pic_out;
   	
   return 0;
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值