首页 > 技术文章 > 统计帧率的几种方法

18fanna 2014-08-01 10:51 原文


class
CFpsSta {public: time_t m_start_time; bool flag; float m_count; float m_last_fps; CFpsSta(); void checkFps(); };
void CFpsSta::checkFps()
{
    time_t current_time=time(NULL);
    double diff=difftime(current_time,m_start_time);
    
    if (diff>=5 && !flag)
    {
        m_last_fps=m_count/diff;
        cout<<m_last_fps<<endl;
        flag=true;
        m_start_time=current_time;
        m_count=0;
    }
    if ( flag)
    {
        flag=false;
    }

    
}

 



每5秒计算一次平均帧率,并清空数值,重新计数;下一次调用时,重设flag; 其中m_count在绘制函数后++。

 

 

第二种

class CFpsSta2
{public:

queue<time_t> counts;

CFpsSta2();
void checkFps();
};
void CFpsSta2::checkFps()
{
    time_t current_time=time(NULL);
    counts.push(current_time);

    double diff=current_time-counts.front();
    //cout<<diff<<endl;
    if(diff>=1)
    {
        cout<<counts.size()/diff<<endl;
        while(!counts.empty())
            counts.pop();    
    }
}

对绘制时间入队列,每次检测到队列首尾时间差大于1秒时 输入size 清空;



  两种方式分别是对帧率计算中的帧数和时间加以控制,第一种是以帧数为主,时间为辅;第二种主要观测时间。

 其他指标在实现时,如果有多个因素,也会有多个计算方法,选择合适的。

推荐阅读