|See what's going on with flipcode!|
Question submitted by (22 November 1999)
|Return to The Archives|
|I'm currently designing an RTS game and I'm wondering how to determine the speed of the processor the game is running on. The faster the processor the more "environment & effect" settings I can implement.|
The bad news is that there's no easy API call to get the processor speed... the
good news is that most games don't need to know that. Instead, they can just
time the number of milliseconds it takes to render one frame, from start to
finish (say, by using Win32's SetTimer() and KillTimer() functions). Then, if
the game knows the maximum amount of time it can spend drawing one frame, it can
easily decide whether or not to add extra detail.
For example, let's say I'd like my game to run at 20 fps, so I tell it that no frame should take more than 50 ms (1000 ms / 20 fps) to render. Let's say the game is running on a wicked fast system, and that the game figures out it only takes 10 ms to render a frame in low detail. It's got an extra 40 ms, so the game decides to bump the detail level up a notch. On this higher detail level, the game notices the frames take about 35 ms to draw. It's still got time to spare, so it ups the detail level one more time, and notices they're now taking 55 ms to draw. Oops, that's too long, so the game drops the level of detail back down a notch, and stays there.
It's an inexact science, but most of the time it gets the job done.
p.s. - I've received a number of comments from people telling me that there are many ways to get the CPU speed - the cpuid assembly instruction, or the QueryPerformanceCounter() API. They are correct; I specifically did not mention these methods because unfortunately, they don't tell the whole story. A game's performance depends on much more than just the speed of the CPU - the bus speed, memory speed, L2 cache size, graphics card acceleration, and a slew of other factors all make a difference. Therefore, simply knowing CPU speed usually isn't enough - you want to know the total system performance, and in my opinion, the best way to measure total system performance is to determine how much time it takes to draw one frame.
Response provided by Mason McCuskey
This article was originally an entry in flipCode's Fountain of Knowledge, an open Question and Answer column that no longer exists.