Hi guys,
Quick one... is it possible to get the cpu speed as e.g. an integer with some sort of function?
I've tried the faq and the search functions...
Thanks in advance.
Printable View
Hi guys,
Quick one... is it possible to get the cpu speed as e.g. an integer with some sort of function?
I've tried the faq and the search functions...
Thanks in advance.
IT is described here ,well not directly :
http://www.cprogramming.com/fod/clock.html
Well if you're using windows, I'm sure it's in the registry somewhere.
Which OS and compiler are you using?
What is your compiler?
The OP's compiler supports windows.h and getch(): http://cboard.cprogramming.com/showp...67&postcount=3
As well as dirent.h and unistd.h: http://cboard.cprogramming.com/showthread.php?t=76928
I would hazard a guess that they use Dev-C++.
Quote:
Originally Posted by dwks
Yes Sherlock :D I'm using Dev-C++, but might switch to Borland quite soon (especially if necessary for this :p) and I'm running Windows XP
Edit: put it in my sig now.
Yes, I've tried that one, but I got 1000 CLOCKS_PER_SEC on both my 1.6 Ghz laptop and a 400 Mhz desktop pc... cant be right imo?Quote:
Originally Posted by kris.c
Actually there is a much more complex method which you can get from http://www.mcshaffry.com/GameCode/, it's in the source code and written by Michael Lyons.
Page 210 in the book Game Coding Complete (had to advertise it since both McShaffry and his book are awesome).
Use ASM for counting cpu cycles.
Why do you need it anyway?
Been Googling for a bit, can't find anything that helps, sorry. Have you maybe got something like an example program?Quote:
Originally Posted by apsync
My program sends CAN messages via usb, pci or pcmcia interfaces to external CAN devices. On my 1.6 Ghz laptop it works fine, but the timing gets messed up when I try the program on our old and slow (400 Mhz) desktop pc. I can fix it by adjusting a couple of counter limits etc. in the source, but since I make updates quite often and the program needs to work on both pc-types, I thought I'd create a formula to make the timing cpu dependent, so I wouldn't need to make the adjustments and separate compiles everytime.Quote:
Originally Posted by siavoshkc
Code:// asm for cpuspeed() (used for counting cpu cycles)
unsigned __int64 cyclecount(void)
{
#if defined (__LCC__) // this code is for lcc
unsigned __int64 count = 0;
_asm ("rdtsc\n"
"mov %eax,%count\n");
return count;
#elif defined (__GNUC__) // this code is for GCC
unsigned __int64 count = 0;
__asm__ ("rdtsc;movl %%eax, %0" : "=r" (count));
return count;
#else // this code is for MSVC, may work on other compilers
_asm {
_emit 0x0F;
_emit 0x31;
}
#endif
}
Code:int cpuspeed(void)
{
unsigned __int64 startcycle;
unsigned __int64 speed, num, num2;
do {
startcycle = cyclecount();
Sleep(1000);
speed = ((cyclecount()-startcycle)/100000)/10;
} while (speed > 1000000); // retry if 1000GHz+
// make it rounder
num = speed % 100;
num2 = 100;
if (num < 80) num2 = 75;
if (num < 71) num2 = 66;
if (num < 55) num2 = 50;
if (num < 38) num2 = 33;
if (num < 30) num2 = 25;
if (num < 10) num2 = 0;
speed = (speed-num)+num2;
return speed;
}
Hope this clears a lot...Code:printf("%dMHz\n", cpuspeed() );
Thanks for the effort apsync! I've tried it, defined GNUC, but I get 0Mhz as output... what am I doing wrong?
Edit:
Code:/******************************** Includes ************************************/
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#include <dirent.h>
/******************************************************************************/
#define GNUC
// asm for cpuspeed() (used for counting cpu cycles)
unsigned __int64 cyclecount(void)
{
#if defined (__LCC__) // this code is for lcc
unsigned __int64 count = 0;
_asm ("rdtsc\n"
"mov %eax,%count\n");
return count;
#elif defined (__GNUC__) // this code is for GCC
unsigned __int64 count = 0;
__asm__ ("rdtsc;movl %%eax, %0" : "=r" (count));
return count;
#else // this code is for MSVC, may work on other compilers
_asm {
_emit 0x0F;
_emit 0x31;
}
#endif
}
int cpuspeed(void)
{
unsigned __int64 startcycle;
unsigned __int64 speed, num, num2;
do {
startcycle = cyclecount();
Sleep(1000);
speed = ((cyclecount()-startcycle)/100000)/10;
} while (speed > 1000000); // retry if 1000GHz+
// make it rounder
num = speed % 100;
num2 = 100;
if (num < 80) num2 = 75;
if (num < 71) num2 = 66;
if (num < 55) num2 = 50;
if (num < 38) num2 = 33;
if (num < 30) num2 = 25;
if (num < 10) num2 = 0;
speed = (speed-num)+num2;
return speed;
}
int main(void)
{
printf("CPU speed: %d MHz\n", cpuspeed() );
return 0;
}
I am not sure, but there should be a way of doing this without need to know CPU clock. I don't know about CAN. But in this cases use this method:
PC---ready?--->Device
PC(waits)
PC<---I am ready--Device
PC------msg-------> Device
PC-----ready?----->Device
PC(waits) Device
PC<---I am ready--Device
PC------msg------> Device
PC<----ready?----- Device
PC Device(waits)
PC<------msg------ Device
Something like this. Maybe it needs interrupts. I don't know how, but I am sure there is a way to do it.
Aha I remembered something. USB works in three modes:
1)Sync 2)Async 3)Bulk
OK? You need Async connection type.
My info is so limited in this area. You should investigate USB programming to find more info. Hope it helps.
Hi siavoshkc,Quote:
Originally Posted by siavoshkc
Thanks so much for taking the time to help/explain. I'm quite sure the USB timing isn't the problem. The CAN devices I'm talking about, are flashed with a special program that enable programming via CAN messages. To enable the programming mode the device needs a certain CAN message which it needs to see within 10 milliseconds after power is switched on. That's the "timing" I'm on about.
Sorry for being a bit vague earlier and thanks again for your input.