统计一段时间内的fps的平均值(C/C++)

统计一段时间内的fps的平均值(C/C++)

帧率(FPS, frame per second)计算是视频处理,算法运行中常见的一个话题,因为表现在画面刷新与视觉感官上,所以相对而言,帧率非常影响用户体验,在一般开发中,开发者也会关注算法或视频相关处理的速度,因此统计FPS是一个比较重要的手段,如下是一种统计一段时间内,即固定时间的帧数统计,算出每秒的帧数,即FPS。
一般有好几种计算方法,列举一以下三种:

  • 固定帧数计算时间
  • 固定时间计算帧数(本文使用)
  • 前后帧时间差计算

代码如下

#include <iostream>
#include <string>
#include <sys/time.h>
#include <unistd.h>

void fps_average_count(int cycle_seconds)
{

	static struct timeval time_start, time_end;
	static bool init = false;
	static int count = 0, fps = 0;

	if (!init)
	{
		init = true;
		gettimeofday(&time_start, NULL);
		gettimeofday(&time_end, NULL);
		printf("FPS count init done ...\n");
	}
	else
	{
		int cycle = time_end.tv_sec - time_start.tv_sec;
		gettimeofday(&time_end, NULL);

		if (cycle == cycle_seconds)
		{
			gettimeofday(&time_start, NULL);
			fps = count;
			count = 0;
			printf("----------\n");
			printf("%d S average FPS value is: %d\n", cycle_seconds, fps / cycle_seconds);
		}
		count++;
	}
}

int main(int argc, char *argv[])
{
	while (1)
	{
		usleep(33333);
		fps_average_count(2);
	}
	return 0;
}

用于统计视频帧率,或者buffer轮转的帧率,可以计算一段时间内的平均帧率!
fps_average_count(2) 代表统计2S 内的平均帧率,如果想要1S的填1就ok啦!

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值