I'm trying to display my frames-per-second in my cube-rendering program. I would like to see its performance. So, how can I do it? I have done research on this already, but the examples I've seen use either multiple classes and still don't work, or they use libraries that I don't have. Is there a way to get the FPS by using pre-installed libs like ctime? I am using OpenGL with C++.
Here is my (empty) function:
void GetFPS()
{
}
and then I display my FPS in my render function with:
std::cout << xRot << " " << yRot << " " << zRot << " " << FPS << "\n"; //xRot, yRot, and zRot are my cube's rotation.
My program is set to 60FPS, but I would like to see the actual FPS, not what it's set to.
Your Windows PC also comes with a built-in FPS counter, thanks to the Windows Game Bar overlay. You can launch this feature with the Win + G shortcut, with performance data—CPU, GPU, VRAM, RAM, and FPS—displayed in the lower-left widget.
Windows 10 comes with a built-in FPS counter. It works with the Windows 10 game bar. You do not have to install anything and you can pin the FPS counter to the screen and use it to monitor the frame rate.
For most people, 60 FPS is the best frame rate to play at. This isn't only because of the smoothness of the images displayed but also because 60Hz monitors are the most readily available ones. Furthermore, acquiring a GPU that can output 60 FPS in a video game is pretty easy and inexpensive nowadays.
You have to sample 2 different time intervals using clock()
however notes that there are several problems:
clock()
you will get 0 many times and at some time you will measure a real time (in my case it just make a jump of 15/16 milliseconds)Do that:
double clockToMilliseconds(clock_t ticks){
// units/(units/time) => time (seconds) * 1000 = milliseconds
return (ticks/(double)CLOCKS_PER_SEC)*1000.0;
}
//...
clock_t deltaTime = 0;
unsigned int frames = 0;
double frameRate = 30;
double averageFrameTimeMilliseconds = 33.333;
while(rendering){
clock_t beginFrame = clock();
render();
clock_t endFrame = clock();
deltaTime += endFrame - beginFrame;
frames ++;
//if you really want FPS
if( clockToMilliseconds(deltaTime)>1000.0){ //every second
frameRate = (double)frames*0.5 + frameRate*0.5; //more stable
frames = 0;
deltaTime -= CLOCKS_PER_SEC;
averageFrameTimeMilliseconds = 1000.0/(frameRate==0?0.001:frameRate);
if(vsync)
std::cout<<"FrameTime was:"<<averageFrameTimeMilliseconds<<std::endl;
else
std::cout<<"CPU time was:"<<averageFrameTimeMilliseconds<<std::endl;
}
}
The above code works also when you do something that takes several seconds. I do a computation that is updated every second, you could as well update it more often. (note I use exactly that code in most of my projects that need FPS)
just call this in any loop to measure the number of calls a second.
#include <chrono>
void printFPS() {
static std::chrono::time_point<std::chrono::steady_clock> oldTime = std::chrono::high_resolution_clock::now();
static int fps; fps++;
if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::high_resolution_clock::now() - oldTime) >= std::chrono::seconds{ 1 }) {
oldTime = std::chrono::high_resolution_clock::now();
std::cout << "FPS: " << fps << std::endl;
fps = 0;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With