计算帧率、每秒过多少次

1、c++

cpp 复制代码
#include <iostream>
#include <opencv2/opencv.hpp>
#include <string>
#include <thread>
#include <atomic>

using namespace std;

const int NUM_THREADS = 1; // 线程数量

std::atomic<int> frameCounts[NUM_THREADS];  // 每个线程的帧计数
std::atomic<bool> running(true);            // 控制定时器的运行状态
std::mutex printMutex;

// 定时器函数,记录每个线程的FPS
void timer(int threadIndex) {
	while (running) {
		// 等待1秒
		std::this_thread::sleep_for(std::chrono::seconds(1));

		// 打印并重置当前线程的帧数
		int fps = frameCounts[threadIndex].exchange(0);
		{
			std::lock_guard<std::mutex> lock(printMutex);
			std::cout << "Thread " << threadIndex << " FPS: " << fps << std::endl;
		}
	}
}

int main() {
	std::thread t(timer, 0);
	t.detach();
	while (true) {
		frameCounts[0]++;
	}
	
	system("pause");
	return 0;
}

输出:

2、Python

python 复制代码
from multiprocessing import Process, Lock, Value
import time

class Timer(Process):
    def __init__(self, lock, crt_time, last_time):
        super().__init__()
        self.current_time = current_time
        self.last_time = last_time
        self.lock = lock
    
    def run(self):
        while True:
            # 等待1s
            time.sleep(1)
            with self.lock:
                self.last_time.value = self.current_time.value
                print('----------num----------:', self.last_time.value)
                self.current_time.value = 0
            

if __name__ == '__main__':
    lock = Lock()
    # 使用 Value 创建共享变量
    current_time = Value('i', 0)  # 'i' 表示整数类型
    last_time = Value('i', 0)

    timer = Timer(lock, current_time, last_time)
    timer.start()
    while True:
        with lock:
            timer.current_time.value += 1

输出:

相关推荐
qq_432703662 分钟前
Golang怎么用embed嵌入SQL文件_Golang如何将SQL迁移文件嵌入Go程序统一管理【技巧】
jvm·数据库·python
m0_640309308 分钟前
如何将 sticky 元素精确定位到父容器的右上角
jvm·数据库·python
带娃的IT创业者11 分钟前
深度解析 Claude Design:如何利用 Anthropic 最新设计范式构建 AI 原生应用
人工智能·python·llm·claude·应用开发·anthropic·ai原生应用
是梦终空14 分钟前
计算机毕业设计271—基于python+深度学习+YOLOV7的车牌识别系统(源代码+数据库+3万字论文)
python·深度学习·opencv·yolo·毕业设计·pyqt5·车牌识别系统
m0_3776182321 分钟前
c++如何将双精度浮点数以科学计数法写入文件_scientific标志【详解】
jvm·数据库·python
weixin_4249993624 分钟前
如何检测SQL注入风险_利用模糊测试技术发现漏洞
jvm·数据库·python
2301_7751481527 分钟前
如何用正则具名捕获组 (-) 提升复杂数据的提取效率
jvm·数据库·python
6Hzlia30 分钟前
【Hot 100 刷题计划】 LeetCode 287. 寻找重复数 | C++ 数组判环 (快慢指针终极解法)
c++·算法·leetcode
2501_9142459332 分钟前
Go语言如何在VSCode中开发_Go语言VSCode配置教程【避坑】.txt
jvm·数据库·python
2301_7826591835 分钟前
MongoDB如果有一个分片完全宕机集群还能用吗_受影响数据的不可读与分片隔离感知
jvm·数据库·python