Mhz des Prozessor auslesen



  • Wie kann man die Mhz des Prozessors auslesen?
    Mit GetSystemInfo habe ich das nicht hinbekommen.
    Und aus der Registry würde ich den Wert echt ungern auslesen.
    Kennt jemand ne andere möglichkeit?



  • int speed = 0;
    time_t t = time(NULL);
    while((t + 1) > time(NULL)
     speed++;
    

    so sollte es gehen



  • #include <windows.h>
    #include <tchar.h>
    #include <lmaccess.h>    // wegen NTSTATUS
    #include <stdio.h>
    
    typedef LRESULT (WINAPI * CALLNTPOWERINFORMATION)(POWER_INFORMATION_LEVEL, PVOID, 
                     ULONG, PVOID, ULONG);
    
    typedef struct _PROCESSOR_POWER_INFORMATION {
        ULONG                   Number;
        ULONG                   MaxMhz;
        ULONG                   CurrentMhz;
        ULONG                   MhzLimit;
        ULONG                   MaxIdleState;
        ULONG                   CurrentIdleState;
    } PROCESSOR_POWER_INFORMATION, *PPROCESSOR_POWER_INFORMATION;
    
    #ifdef __BORLANDC__
      #define EMIT   db
    #else
      #define EMIT   __emit
    #endif
    
    #if defined(__cplusplus) && !defined(INLINE)
      #define INLINE inline
    #else
      #define INLINE
    #endif
    
    INLINE UINT64 rdtsc(VOID)
    {
        _asm EMIT 0x0F
        _asm EMIT 0x31
    }
    
    UINT_PTR GetCpuSpeed(VOID)
    {
      PROCESSOR_POWER_INFORMATION ppi;
      CALLNTPOWERINFORMATION      pfn;
      LARGE_INTEGER               liFreq, li1, li2;
      HINSTANCE                   hDll;
      NTSTATUS                    stat;
      UINT_PTR                    uSpeed;
      UINT64                      timestamp;
      HANDLE                      hThread;
      int                         nPriority;
    
        ZeroMemory(&ppi, sizeof(ppi));
        stat = ERROR_CALL_NOT_IMPLEMENTED;
    
        if(NULL != (hDll = LoadLibrary(TEXT("powrprof.dll"))))
        {
            if(NULL != (pfn = (CALLNTPOWERINFORMATION)GetProcAddress(hDll, 
                               "CallNtPowerInformation")))
            {
                stat = pfn(ProcessorInformation, NULL, 0, &ppi, sizeof(ppi));
            }
            FreeLibrary(hDll);
        }
    
        if(ERROR_SUCCESS == stat)
            return(ppi.CurrentMhz);
    
        hThread   = GetCurrentThread();
        nPriority = GetThreadPriority(hThread);
    
        QueryPerformanceFrequency(&liFreq);
        liFreq.QuadPart /= 10;
        SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST);
    
        timestamp = rdtsc();
        QueryPerformanceCounter(&li1);
    
        do
        {
            QueryPerformanceCounter(&li2);
        } while((li2.QuadPart - li1.QuadPart) < liFreq.QuadPart);
    
        uSpeed = (UINT_PTR)((rdtsc() - timestamp) / 100000);
        SetThreadPriority(hThread, nPriority);
    
      return(uSpeed);
    }
    
    int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
    {
      TCHAR szSpeed[128];
    
        wsprintf(szSpeed, TEXT("CPU-Speed: ~%uMHz\n"), GetCpuSpeed());
        MessageBox(NULL, szSpeed, NULL, MB_OK);
    
      return(0);
    }
    

    Quelle: http://www.winapi.net (nicht erreichbar)



  • Geht doch viel einfacher:

    /*UINT64 rdtsc()   //Für MSV C++
    {
        __asm _emit 0x0F
        __asm _emit 0x31
    }*/
    
    UINT64 rdtsc()     //Für Dev-C++
    {
        UINT64 tmp;
        __asm volatile("rdtsc" : "=A" (tmp));
    return tmp;
    }
    
    float GetCPUSpeed()
    {
        LARGE_INTEGER liFreq, liTicks, liValue;
        UINT64        iTimestamp;
        float         fSpeed;    
        HANDLE        hThread;
        int           iPriority;
    
        hThread  = GetCurrentThread();
        iPriority = GetThreadPriority(hThread);
    
        QueryPerformanceFrequency(&liFreq);
    
        QueryPerformanceCounter(&liTicks);
        liValue.QuadPart = liTicks.QuadPart + liFreq.QuadPart;
    
        SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST);
    
        iTimestamp = rdtsc();
    
        do 
        {
          QueryPerformanceCounter(&liTicks);
        } while (liTicks.QuadPart <= liValue.QuadPart);
    
        fSpeed = (float)(int)(rdtsc() - iTimestamp)/ 1000000;
        SetThreadPriority(hThread, iPriority);
    
        return fSpeed;
    }
    

    Quelle: http://www.winapi.net (nicht erreichbar)

    Noe stimmt nicht, guckst du archive ->



  • oh man bist du wired



  • weird



  • wo wir grad bei dem thema sind

    wie kann ich die anzahl der prozessor kerne auslesen???



  • wie kann ich die anzahl der prozessor kerne auslesen???

    GetSystemInfo



  • der quelltext von real_helper liefert bei mir das richtigere ergebnis, während der von WiredCoder nur 1800 ergibt gibt der erstere 2399 zurück. CPU ist ein Core2Duo E6600 2400 MHz.



  • also nimmst du den von Wired ..

    @laoxi: "Mhz" (besser: MHz) ist nur eine Einheit für die Frequenz, in dem Fall meist Taktfrequenz (weil sie die Anzahl der Takte pro Sekunde angibt) genannt, also bitte ... du liest nicht die "MHz" aus, sondern die Taktfrequenz (in MHz). Fürs nächste Mal und die Male danach xD ...



  • @real_helper

    ...
    Sleep(90);
    do
    ...
    

    kann die cpu usage merklich reduzieren 😋


Anmelden zum Antworten