#include <cstdlib>
#include <iostream>
#include <chrono>
#include <ctime>
#include <queue>
using namespace std;
 
int main()
{
    // your code goes here
 
    std::chrono::steady_clock::time_point t0, t1, tp;
    int isNotFirst = 0;
    double elapsed_s = 0, g_samples_s = 0;
    std::queue<double> g_fps;
    size_t nb_samples;
 
    for (int i = 0; i < 10; ++i)
    {
        t0 = std::chrono::steady_clock::now();
 
        int r = (std::rand() % 10) + 25; // 25 ~ 34
        std::this_thread::sleep_for(std::chrono::milliseconds(r));
 
        t1 = std::chrono::steady_clock::now();
        elapsed_s = (double)r / 1000;//std::chrono::duration_cast<std::chrono::duration<double>(t1 - t0).count();
 
        double fps = 0, avg_fps = 0;
 
        if (isNotFirst)
        {
            nb_samples = g_fps.size();
            elapsed_s = r;//std::chrono::duration_cast<std::chrono::duration<double>>(t0 - tp).count();
 
            g_samples_s += elapsed_s;
            g_fps.push(elapsed_s);
 
            if (nb_samples >= 10)
            {
                g_samples_s -= g_fps.front();
                   g_fps.pop();
            }
            else nb_samples++;
 
            fps = 1 / elapsed_s;
            avg_fps = nb_samples / g_samples_s;
        }
 
        tp = t0;
        isNotFirst = 1;
 
        printf("%d. %.2f/%.2f fps\r\n", i, fps, avg_fps);
    }
 
    return 0;
}

'C, C++' 카테고리의 다른 글

xlnt - XLSX 파일 다루기  (0) 2022.12.22
To install the MinGW-w64 toolchain  (0) 2022.10.28
문자열 구분자로 분리  (0) 2021.10.20
VSCode + vcpkg  (0) 2021.10.19
Get DLL path at run time  (0) 2021.10.05

+ Recent posts