MHz CPU ermitteln



  • Wie kann ich die cpu velocity in MHz ermitteln?



  • Mit Standard C++ garnicht. Welche Plattform?



  • In Standard-C++ geht nichts Interessantes, ist völlig autistisch. 😉
    Wenn du MS Windows verwendest, geht z.B. dies hier:

    #include <conio.h>
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    /*UINT64 rdtsc()   // MSVC++
    {
        __asm _emit 0x0F
        __asm _emit 0x31
    }*/
    
    UINT64 rdtsc()     // 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;
    }
    
    int main()
    {
      cout << GetCPUSpeed() << " MHz";
      getch();
    }
    


  • theoretisch könnte man das doch auf jeder Platform machen. Gibt es entsprechende Timer (bzw libs), die ähnlich präzise sind wie der QueryPerformanceCounter, auch für andere Systeme?



  • fuzzy_bear schrieb:

    Wie kann ich die cpu velocity in MHz ermitteln?

    Ich würde es vom System abfragen. Unter Linux zum Beispiel steht es in /proc/cpuinfo



  • randa schrieb:

    theoretisch könnte man das doch auf jeder Platform machen. Gibt es entsprechende Timer (bzw libs), die ähnlich präzise sind wie der QueryPerformanceCounter, auch für andere Systeme?

    das würd mich auch sehr interesieren, ob es sowas wie QueryPerformanceCounter() auch plattformunabhänig gibt

    das google tutorial ist ja schön muss aber ewig suchen, also wenn jemand das schon weis, pls posten 🙂



  • Unter Linux steht der Kram in /proc/cpuinfo. In C sähe das so aus:

    #include <stdio.h>
    
    double cpu_frequency() {
      FILE *fd = fopen("/proc/cpuinfo", "r");
      char buf[64];
      double freq = 0.0;
    
      if(fd) {
        while(fgets(buf, 64, fd))
          if(strstr(buf, "cpu MHz") == buf)
            break;
        sscanf(buf, "cpu MHz : %lf", &freq);
    
        fclose(fd);
      }
    
      return freq;
    }
    

    ...und für C++ hab ich grad keine Lust, das nachzucoden.


Anmelden zum Antworten