Linux测试处理fps为30、1920*1080、一分钟的视频性能

embedded/2025/1/21 11:55:42/

前置条件

模拟fps为30、1920*1080、一分钟的视频

项目CMakeLists.txt

cmake_minimum_required(VERSION 3.30)
project(testOpenGl)set(CMAKE_CXX_STANDARD 11)add_executable(testOpenGl main.cpptestOpenCl.cpptestOpenCl.hTestCpp.cppTestCpp.hTestCppThread.cppTestCppThread.hTestSIMD.cppTestSIMD.h)# 查找OpenCL
find_package(OpenCL REQUIRED)# 链接OpenCl库
target_include_directories(testOpenGl PRIVATE ${OpenCL_INCLUDE_DIRS})
target_link_libraries(testOpenGl PRIVATE ${OpenCL_LIBRARIES})# 检测SIMD支持并添加编译选项
include(CheckCXXCompilerFlag)check_cxx_compiler_flag("-mavx" COMPILER_SUPPORTS_AVX)
check_cxx_compiler_flag("-mavx2" COMPILER_SUPPORTS_AVX2)if(COMPILER_SUPPORTS_AVX2)target_compile_options(testOpenGl PRIVATE -mavx2)
elseif (COMPILER_SUPPORTS_AVX)target_compile_options(testOpenGl PRIVATE -mavx)
else ()message(FATAL_ERROR "AVX or AVX2 is not supported by compiler")
endif ()

C++代码

//
// Created by lai on 2025/1/17.
//#include "TestCpp.h"#include <iostream>
#include <vector>
#include <random>
#include <chrono>// 灰度转换函数
void to_gray(const std::vector<unsigned char>& input, std::vector<unsigned char>& output, int width, int height) {for (int i = 0; i < width * height; ++i) {int offset = i * 3;  // RGB 分量unsigned char r = input[offset];unsigned char g = input[offset + 1];unsigned char b = input[offset + 2];// 灰度公式output[i] = static_cast<unsigned char>(0.299f * r + 0.587f * g + 0.114f * b);}
}
void TestCpp::runTest() {const int width = 1920;         // 视频宽度const int height = 1080;        // 视频高度const int fps = 30;             // 帧率const int duration = 60;        // 视频持续时间(秒)const int frameCount = fps * duration; // 总帧数// 模拟视频帧数据:随机生成每帧的 RGB 数据std::vector<unsigned char> inputFrame(width * height * 3);std::vector<unsigned char> outputFrame(width * height);std::random_device rd;std::mt19937 gen(rd());std::uniform_int_distribution<> dis(0, 255);// 开始处理auto startTime = std::chrono::high_resolution_clock::now();for (int frame = 0; frame < frameCount; ++frame) {// 随机生成模拟的 RGB 数据for (auto& pixel : inputFrame) {pixel = dis(gen);}// 调用灰度转换函数to_gray(inputFrame, outputFrame, width, height);// 打印进度if (frame % 30 == 0) {std::cout << "Processed frame: " << frame + 1 << "/" << frameCount << std::endl;}}auto endTime = std::chrono::high_resolution_clock::now();double elapsedTime = std::chrono::duration<double>(endTime - startTime).count();// 打印处理时间std::cout << "Processed " << frameCount << " frames in " << elapsedTime << " seconds." << std::endl;std::cout << "Average time per frame: " << (elapsedTime / frameCount) << " seconds." << std::endl;}

C++多线程

//
// Created by lai on 2025/1/17.
//#include "TestCppThread.h"#include <iostream>
#include <vector>
#include <random>
#include <chrono>
#include <thread>// 灰度转换函数,每个线程处理一部分图像
void to_gray_chunk(const std::vector<unsigned char>& input, std::vector<unsigned char>& output, int width, int height, int start, int end) {for (int i = start; i < end; ++i) {int offset = i * 3;  // RGB 分量unsigned char r = input[offset];unsigned char g = input[offset + 1];unsigned char b = input[offset + 2];// 灰度公式output[i] = static_cast<unsigned char>(0.299f * r + 0.587f * g + 0.114f * b);}
}void TestCppThread::runTest() {const int width = 1920;         // 视频宽度const int height = 1080;        // 视频高度const int fps = 30;             // 帧率const int duration = 60;        // 视频持续时间(秒)const int frameCount = fps * duration; // 总帧数const int numThreads = std::thread::hardware_concurrency(); // 获取可用线程数// 模拟视频帧数据:随机生成每帧的 RGB 数据std::vector<unsigned char> inputFrame(width * height * 3);std::vector<unsigned char> outputFrame(width * height);std::random_device rd;std::mt19937 gen(rd());std::uniform_int_distribution<> dis(0, 255);// 开始处理auto startTime = std::chrono::high_resolution_clock::now();for (int frame = 0; frame < frameCount; ++frame) {// 随机生成模拟的 RGB 数据for (auto& pixel : inputFrame) {pixel = dis(gen);}// 启动多个线程来处理图像std::vector<std::thread> threads;int chunkSize = width * height / numThreads; // 每个线程处理的像素块大小for (int t = 0; t < numThreads; ++t) {int start = t * chunkSize;int end = (t == numThreads - 1) ? (width * height) : (start + chunkSize); // 最后一个线程处理剩余的像素threads.emplace_back(to_gray_chunk, std::cref(inputFrame), std::ref(outputFrame), width, height, start, end);}// 等待所有线程完成for (auto& t : threads) {t.join();}// 打印进度if (frame % 30 == 0) {std::cout << "Processed frame: " << frame + 1 << "/" << frameCount << std::endl;}}auto endTime = std::chrono::high_resolution_clock::now();double elapsedTime = std::chrono::duration<double>(endTime - startTime).count();// 打印处理时间std::cout << "Processed " << frameCount << " frames in " << elapsedTime << " seconds." << std::endl;std::cout << "Average time per frame: " << (elapsedTime / frameCount) << " seconds." << std::endl;}

CPU版本的Opencl

cmake中添加

# 查找OpenCL
find_package(OpenCL REQUIRED)# 链接OpenCl库
target_include_directories(testOpenGl PRIVATE ${OpenCL_INCLUDE_DIRS})
target_link_libraries(testOpenGl PRIVATE ${OpenCL_LIBRARIES})

测试代码

//
// Created by lai on 2025/1/16.
//
#include "testOpenCl.h"#include <chrono>
#include <CL/cl.h>
#include <iostream>
#include <vector>
#include <random>// OpenCL 内核代码
const char* kernelSource = R"(
__kernel void to_gray(__global unsigned char* input,__global unsigned char* output,const int width,const int height)
{int id = get_global_id(0);  // 每个线程处理一个像素if (id < width * height) {int offset = id * 3;  // RGB 分量unsigned char r = input[offset];unsigned char g = input[offset + 1];unsigned char b = input[offset + 2];// 灰度公式output[id] = (unsigned char)(0.299f * r + 0.587f * g + 0.114f * b);}
}
)";
void TestOpenCl::runTests() {const int width = 1920;         // 视频宽度const int height = 1080;        // 视频高度const int fps = 30;             // 帧率const int duration = 60;        // 视频持续时间(秒)const int frameCount = fps * duration; // 总帧数// 模拟视频帧数据:随机生成每帧的 RGB 数据std::vector<unsigned char> inputFrame(width * height * 3);std::vector<unsigned char> outputFrame(width * height);std::random_device rd;std::mt19937 gen(rd());std::uniform_int_distribution<> dis(0, 255);// 初始化 OpenCLcl_int err;cl_platform_id platform;clGetPlatformIDs(1, &platform, nullptr);cl_device_id device;clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 1, &device, nullptr);cl_context context = clCreateContext(nullptr, 1, &device, nullptr, nullptr, &err);cl_command_queue queue = clCreateCommandQueue(context, device, 0, &err);cl_program program = clCreateProgramWithSource(context, 1, &kernelSource, nullptr, &err);clBuildProgram(program, 1, &device, nullptr, nullptr, nullptr);cl_kernel kernel = clCreateKernel(program, "to_gray", &err);// 创建 OpenCL 缓冲区cl_mem inputBuffer = clCreateBuffer(context, CL_MEM_READ_ONLY, inputFrame.size(), nullptr, &err);cl_mem outputBuffer = clCreateBuffer(context, CL_MEM_WRITE_ONLY, outputFrame.size(), nullptr, &err);// 开始处理auto startTime = std::chrono::high_resolution_clock::now();for (int frame = 0; frame < frameCount; ++frame) {// 随机生成模拟的 RGB 数据for (auto& pixel : inputFrame) {pixel = dis(gen);}// 写入数据到 OpenCL 缓冲区clEnqueueWriteBuffer(queue, inputBuffer, CL_TRUE, 0, inputFrame.size(), inputFrame.data(), 0, nullptr, nullptr);// 设置内核参数clSetKernelArg(kernel, 0, sizeof(cl_mem), &inputBuffer);clSetKernelArg(kernel, 1, sizeof(cl_mem), &outputBuffer);clSetKernelArg(kernel, 2, sizeof(int), &width);clSetKernelArg(kernel, 3, sizeof(int), &height);// 定义工作区大小size_t globalSize = width * height;// 执行内核clEnqueueNDRangeKernel(queue, kernel, 1, nullptr, &globalSize, nullptr, 0, nullptr, nullptr);// 读取处理后的灰度数据clEnqueueReadBuffer(queue, outputBuffer, CL_TRUE, 0, outputFrame.size(), outputFrame.data(), 0, nullptr, nullptr);// 打印进度if (frame % 30 == 0) {std::cout << "Processed frame: " << frame + 1 << "/" << frameCount << std::endl;}}auto endTime = std::chrono::high_resolution_clock::now();double elapsedTime = std::chrono::duration<double>(endTime - startTime).count();// 打印处理时间std::cout << "Processed " << frameCount << " frames in " << elapsedTime << " seconds." << std::endl;std::cout << "Average time per frame: " << (elapsedTime / frameCount) << " seconds." << std::endl;// 释放 OpenCL 资源clReleaseMemObject(inputBuffer);clReleaseMemObject(outputBuffer);clReleaseKernel(kernel);clReleaseProgram(program);clReleaseCommandQueue(queue);clReleaseContext(context);
}

内存对齐的SIMD指令集

cmake添加

# 检测SIMD支持并添加编译选项
include(CheckCXXCompilerFlag)check_cxx_compiler_flag("-mavx" COMPILER_SUPPORTS_AVX)
check_cxx_compiler_flag("-mavx2" COMPILER_SUPPORTS_AVX2)if(COMPILER_SUPPORTS_AVX2)target_compile_options(testOpenGl PRIVATE -mavx2)
elseif (COMPILER_SUPPORTS_AVX)target_compile_options(testOpenGl PRIVATE -mavx)
else ()message(FATAL_ERROR "AVX or AVX2 is not supported by compiler")
endif ()
//
// Created by lai on 2025/1/17.
//#include "TestSIMD.h"#include <iostream>
#include <vector>
#include <random>
#include <chrono>
#include <immintrin.h> // SIMD 指令集
#include <cstdlib>  // 用于posix_memalignvoid to_gray_simd(const unsigned char* input, unsigned char* output, int width, int height) {const int pixelCount = width * height;const __m256 scale_r = _mm256_set1_ps(0.299f); // 红色通道的权重const __m256 scale_g = _mm256_set1_ps(0.587f); // 绿色通道的权重const __m256 scale_b = _mm256_set1_ps(0.114f); // 蓝色通道的权重int i = 0;for (; i <= pixelCount - 8; i += 8) {// 加载 8 组 RGB 像素__m256i pixel_r = _mm256_loadu_si256((__m256i*)&input[i * 3]);  // 确保内存对齐__m256i pixel_g = _mm256_loadu_si256((__m256i*)&input[i * 3 + 1]);__m256i pixel_b = _mm256_loadu_si256((__m256i*)&input[i * 3 + 2]);// 转换为浮点数以便计算__m256 r_f = _mm256_cvtepi32_ps(pixel_r);__m256 g_f = _mm256_cvtepi32_ps(pixel_g);__m256 b_f = _mm256_cvtepi32_ps(pixel_b);// 灰度转换公式__m256 gray_f = _mm256_add_ps(_mm256_add_ps(_mm256_mul_ps(r_f, scale_r), _mm256_mul_ps(g_f, scale_g)),_mm256_mul_ps(b_f, scale_b));// 转回整数__m256i gray_i = _mm256_cvtps_epi32(gray_f);// 存储结果_mm256_storeu_si256((__m256i*)&output[i], gray_i);}// 处理剩余像素(非对齐部分)for (; i < pixelCount; ++i) {int offset = i * 3;unsigned char r = input[offset];unsigned char g = input[offset + 1];unsigned char b = input[offset + 2];output[i] = static_cast<unsigned char>(0.299f * r + 0.587f * g + 0.114f * b);}
}void TestSIMD::runTest() {const int width = 1920;         // 视频宽度const int height = 1080;        // 视频高度const int fps = 30;             // 帧率const int duration = 60;        // 视频持续时间(秒)const int frameCount = fps * duration; // 总帧数size_t size = width * height * 3 * sizeof(unsigned char);// 模拟视频帧数据:随机生成每帧的 RGB 数据// 使用posix_memalign分配对齐内存unsigned char* inputFrame;unsigned char* outputFrame;int alignment = 32; // 使用32字节对齐int resultInput = posix_memalign((void**)&inputFrame, alignment, size);int resultOutput = posix_memalign((void**)&outputFrame, alignment, size);if (resultInput != 0 || resultOutput != 0) {std::cerr << "memory allocation failed" << std::endl;return;}std::random_device rd;std::mt19937 gen(rd());std::uniform_int_distribution<> dis(0, 255);// 开始处理auto startTime = std::chrono::high_resolution_clock::now();for (int frame = 0; frame < frameCount; ++frame) {// 随机生成模拟的 RGB 数据for (int i = 0; i < width * height * 3; ++i) {inputFrame[i] = dis(gen);}// 使用 SIMD 转换灰度to_gray_simd(inputFrame, outputFrame, width, height);// 打印进度if (frame % 30 == 0) {std::cout << "Processed frame: " << frame + 1 << "/" << frameCount << std::endl;}}auto endTime = std::chrono::high_resolution_clock::now();double elapsedTime = std::chrono::duration<double>(endTime - startTime).count();// 打印处理时间std::cout << "Processed " << frameCount << " frames in " << elapsedTime << " seconds." << std::endl;std::cout << "Average time per frame: " << (elapsedTime / frameCount) << " seconds." << std::endl;
}

结论

C++
Processed 1800 frames in 251.789 seconds.
Average time per frame: 0.139883 seconds.C++ thread
Processed 1800 frames in 229.571 seconds.
Average time per frame: 0.12754 seconds.CPU版本POCL的OPENCL
Processed 1800 frames in 233.25 seconds.
Average time per frame: 0.129583 seconds.SIMD 内存对齐以后
Processed 1800 frames in 191.015 seconds.
Average time per frame: 0.106119 seconds.

SIMD的性能明显由于其他几项,但是还需要测试GPU版本的OPencl和多线程指令集优化对性能的提升


http://www.ppmy.cn/embedded/155763.html

相关文章

uniapp 自定义日历组件 源码

效果图&#xff1a; 一、问题1&#xff1a;每个月的1号&#xff0c;样式上的起始位置 样式上来说实际困难点在于每个月的1号对应的位置&#xff1a; 解决方式就是判断1号是周几&#xff0c;就在前面放几个空盒子&#xff0c; 二、问题2 : 状态样式控制 定义一个结构来存储…

vue使用教程

‌Vue.js主要基于MVVM模式&#xff08;Model-View-ViewModel&#xff09;和观察者模式‌。MVVM模式是Vue.js的核心设计模式&#xff0c;它将应用程序的数据&#xff08;Model&#xff09;、用户界面&#xff08;View&#xff09;和业务逻辑&#xff08;ViewModel&#xff09;分…

使用 Parcel 和 NPM 脚本进行打包

使用 Parcel 和 NPM 脚本进行打包 Parcel Parcel 是一个零配置的网页应用程序打包工具&#xff0c;主要用于快速构建现代 JavaScript 应用。 我们可以使用npm直接安装它 npm install --save-dev parcel //这将把 Parcel 添加到 devDependencies 中&#xff0c;表明它是一个…

【Leetcode 热题 100】70. 爬楼梯

问题背景 假设你正在爬楼梯。需要 n n n 阶你才能到达楼顶。 每次你可以爬 1 1 1 或 2 2 2 个台阶。你有多少种不同的方法可以爬到楼顶呢&#xff1f; 数据约束 1 ≤ n ≤ 45 1 \le n \le 45 1≤n≤45 解题过程 昨天刚刚当成扩展题做过&#xff0c;今天遇到了再写一次。…

C++,设计模式,【目录篇】

文章目录 1. 简介2. 设计模式的分类2.1 创建型模式&#xff08;Creational Patterns&#xff09;&#xff1a;2.2 结构型模式&#xff08;Structural Patterns&#xff09;&#xff1a;2.3 行为型模式&#xff08;Behavioral Patterns&#xff09;&#xff1a; 3. 使用设计模式…

redis.call()和redis.pcall()的区别

redis.call() 与 redis.pcall()很类似, 他们唯一的区别是当redis命令执行结果返回错误时&#xff0c; redis.call()将返回给调用者一个错误&#xff0c;而redis.pcall()会将捕获的错误以Lua表的形式返回 redis.call() 和 redis.pcall() 两个函数的参数可以是任意的 Redis 命令&…

PHP语言的语法糖

PHP语言的语法糖 引言 在现代编程语言中&#xff0c;语法糖&#xff08;Syntactic Sugar&#xff09;是指一些改善用户体验的语言特性&#xff0c;使得某些语法更加简洁、易用。PHP作为一种广泛使用的服务器端脚本语言&#xff0c;自然也不例外。它大量引入了语法糖&#xff…

gitlab使用多数据库

1. 说明 默认情况下&#xff0c;GitLab 使用一个单一的应用数据库&#xff0c;称为主数据库。为了扩展 GitLab&#xff0c;您可以将 GitLab 配置为使用多个应用数据库。 设置多个数据库后&#xff0c;GitLab 将使用第二个应用数据库用于 CI/CD 功能&#xff0c;称为 CI 数据库…