Thread: cpu speed ?

  1. #1
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154

    cpu speed ?

    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.
    Last edited by rkooij; 07-24-2006 at 09:20 AM.
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  2. #2
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    IT is described here ,well not directly :

    http://www.cprogramming.com/fod/clock.html

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well if you're using windows, I'm sure it's in the registry somewhere.
    Which OS and compiler are you using?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    What is your compiler?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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++.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by dwks
    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++.

    Yes Sherlock I'm using Dev-C++, but might switch to Borland quite soon (especially if necessary for this ) and I'm running Windows XP

    Edit: put it in my sig now.
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  7. #7
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by kris.c
    IT is described here ,well not directly :

    http://www.cprogramming.com/fod/clock.html
    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?
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  8. #8
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    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).

  9. #9
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    Use ASM for counting cpu cycles.

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Why do you need it anyway?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  11. #11
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by apsync
    Use ASM for counting cpu cycles.
    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 siavoshkc
    Why do you need it anyway?
    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.
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  12. #12
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    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;
     }
    Code:
    printf("%dMHz\n", cpuspeed() );
    Hope this clears a lot...

  13. #13
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    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;
    }
    Last edited by rkooij; 07-25-2006 at 07:19 AM.
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  14. #14
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    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.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  15. #15
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by siavoshkc
    ....
    Hi 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.
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions on multiple thread programming
    By lehe in forum C Programming
    Replies: 11
    Last Post: 03-27-2009, 07:44 AM
  2. Upgrading my old CPU (for another old one!)
    By foxman in forum Tech Board
    Replies: 16
    Last Post: 01-11-2008, 05:41 PM
  3. CPU temp
    By PING in forum Tech Board
    Replies: 5
    Last Post: 01-28-2006, 06:25 AM
  4. getting maximum CPU speed
    By 13th Phazer in forum C++ Programming
    Replies: 14
    Last Post: 10-20-2004, 06:41 PM
  5. How do I discover the CPU Speed?
    By Scripter in forum C Programming
    Replies: 4
    Last Post: 10-27-2002, 01:38 PM