記部落格開通,以及三天內的兩個編程經驗(CPU頻率與計時、windows電源管理API)

來源:互聯網
上載者:User
文章目錄
  • Not related to CPU frequency in general

CSDN部落格太不給力了,沒事刪我文章,想啥呢。

本部落格從今起記錄我在IT方面學習的道路上積累的經驗,為以後回首留下美好記憶,也為其他遇到與我一樣困難的人分享經驗。

 

先記下三天前想寫的兩個內容

 

一、windows的電源管理API

 

VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime){    std::cout << "hello" << std::endl;}int main(int argc, char *argv[]) {    SYSTEM_POWER_STATUS spsPwr;    int timer1 = 1;    HWND hwndTimer;       MSG msg;              SetTimer(NULL,timer1,500,NULL);    while (GetMessage(&msg,NULL,NULL,NULL)!= 0)    {         if (msg.message == WM_TIMER)         {             if(GetSystemPowerStatus(&spsPwr))            {                if(static_cast<double>(spsPwr.ACLineStatus)!=1)                {                    system("d:\\ttplayer\\TTPlayer.exe \"c:\\a.mp3\"");                }            }        }     }    return 0;}

 

GetSystemPowerStatus(&SYSTEM_POWER_STATUS),能返回當前AC是否接入,電池是否接入,電池電量等資訊。

 

二、關於精確計時,常用CPU時間戳記和CPU頻率來完成,我一直疑惑CPU的頻率是會動態調節的,這樣計時還是否準確。測試了一下,不會。

    LARGE_INTEGER litmp;    LONGLONG QPart1;    double dfFreq;    //獲得計時器的時鐘頻率    QueryPerformanceFrequency(&litmp);    dfFreq = (double)litmp.QuadPart;    QueryPerformanceCounter(&litmp);    QPart1 = litmp.QuadPart;

 

這裡用QueryPerformanceFrequence得到的頻率值非常穩 定,絲毫不變。在我的電腦上是2.2GHz(2208009),有意思的是我電腦標稱頻率為2.26GHz(266*8.5),並在CPU-Z和win8 工作管理員中都顯示2.26GHz。微軟msdn中說

 

Not related to CPU frequency in generalThe high frequency counter need not be tied to the CPU frequency at all. It will only resemble the CPU frequency is the system actually uses the TSC (TimeStampCounter) underneath. As the TSC is generally unreliable on multi-core systems it tends not to be used. When the TSC is not used the ACPI Power Management Timer (pmtimer) may be used. You can tell if your system uses the ACPI PMT by checking if QueryPerformanceFrequency returns the signature value of 3,579,545 (ie 3.57MHz). If you see a value around 1.19Mhz then your system is using the old 8245 PIT chip. Otherwise you should see a value approximately that of your CPU frequency (modulo any speed throttling or power-management that might be in effect.)

If you have a newer system with an invariant TSC (ie
constant frequency TSC) then that is the frequency that will be
returned (if Windows uses it). Again this is not necessarily the CPU
frequency.

也就是這個頻率和CPU頻率沒什麼關係,這隻是時間戳記頻率。時間戳記頻率是恒定的。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.