Hi all,

Yesterday I ran into RDTSC, very useful to have knowledge of, especially when you are using performance counters and timers etc. RDTSC will return a value to you (64 bit) which contains the current clock cycle of your processor.

I have made a little program which requests the RDTSC opcode twice with some time in between using the Sleep(ms); function. Subtracting the two values obtained from RDTSC you can calculate the clockcycles per second, meaning Hz. Dividing this value by one million you calculate the Mhz of the CPU in the time between requesting the clockcycles with RDTSC.

This way you can actually get the current speed of the processor is working on and not what it can handle maximum. I ran this program several times, with and without stressing the CPU. With stressing the CPU to the max my program returned 1662 Mhz (and it is a 1,66 Ghz processor). Without stressing the CPU it is funny to see that it only uses about 670 Mhz, when I adjust the power manage profile to EnergySaving it is lowered to 450Mhz.

Grtz Devoney

The code I have made is for a console application, made with VS2005.

// CPUSpeed.cpp : Defines the entry point for the console application.
#include <stdafx.h>
#include <iostream>
#include <string>
#include <Windows.h>
#include <stdio.h>
#include <winbase.h>
#include <winuser.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	system("cls"); //Clean the console screen
	SetConsoleTitleA("CPU Current Working Speed - by Devoney"); //Set the title of the console screen
	__int64 Answer = 0; //Make a 64-bit, way to large, variable.

	//Output some text describing the program and offering the user to choose the time interval.
	cout << "CPU Current Working Speed\n\nThis program will measure how many clockcycles have passed during a time interval. Then it will calculate how many clockcycles this have been per second and convert the value in Mhz. The program will NOT measure the speed of the CPU in Ghz at which is clocked on, but will measure it's current working speed while executing this program.\n\n";
	cout << "Select your time interval: \n1. 1sec\n2. 2sec\n3. 5sec\n4. 10sec\n5. 30sec\n\nAnswer: ";
	
	cin >> Answer; //retrieve answer

	//Check which answer was given and redefine Answer for later use in the Sleep() function.
	//Notice that their is no error handler when wrong input is given.
	if(Answer == 1)
	{
		Answer = 1000;
	}
	if(Answer == 2)
	{
		Answer = 2000;
	}
		if(Answer == 3)
	{
		Answer = 5000;
	}
	if(Answer == 4)
	{
		Answer = 10000;
	}
	if(Answer == 5)
	{
		Answer = 30000;
	}

	//Initialize 3 64-bit variables, the returned value of RDTSC is 64 bit. We need to obtain it twice and substract them.
	__int64 unsigned BeginClock = 0;
	__int64 unsigned EndClock = 0;
	__int64 unsigned Speed = 0;
	cout << "\nVariables initialized...\n";
	
	//Try to set the priority of the program to the highest possible, realtime.
	if(SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS))
	{
		cout << "Priority Class Set To Realtime...\n";
	}
	else
	{
		cout << "Priority Class Could Not Be Set To Realtime...\n";
	}

	cout << "Waiting for interval to pass...\n\n";
	_asm
	{
		RDTSC //Execute the opcode RDTSC
		mov DWORD PTR SS:[BeginClock], EAX //Move the bytes stored in EAX to the variable BeginClock
		mov DWORD PTR SS:[BeginClock+4], EDX //Move the most significant bytes stored in EDX to BeginClock
	}
	
		Sleep(Answer); //Wait the chosen amount of time

	_asm
	{
		//Again use RDTSC
		RDTSC
		mov DWORD PTR SS:[EndClock], EAX
		mov DWORD PTR SS:[EndClock+4], EDX
	}


	Speed  = (EndClock - BeginClock); //Substract the two RDTSC values to obtain the clockcycles done in the time interval
	cout << "Clockcycles since reset (before interval): " << BeginClock << "\nClockcycles since reset (after interval):  " << EndClock << "\nAmount of Clockcycles after substraction:  " << Speed << "\n";
	Speed = Speed / (1000000 * (Answer/1000)); //Convert the clockcycles to Mhz using the time interval and 1,000,000 constant because otherwise the speed is given in Hz
	cout << "Measured CPU Speed: " << Speed << "Mhz\nProgram Terminated\n";
	return 0; //Exit the program
}

Hi, thanks for the post, could you please tell me in which PL do I need to run this code since I have tried both with C++ and C# compilers, both didn't work. Or is that the problem with some missing header files or sth ?
to keep it short :) I need to know what I need to have for this code running ?
Thanks in advance

Hi there,
I have made this code in Visual Studio 2005 C++. The syntax of VS is probably a bit different then other C++ writers/compilers.
Hopefully you manage.

commented: ++rep for coming back after more then a year to help someone +22

See MSDN and Intel codes samples for RDTSC instead.
More than 10 years old...

commented: Why don't you stop posting this useles information -4
commented: Ohhh, enlighten us with your wisdom. Just stop it, it doesn't make any sense. You're totally useless!! -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.