记博客开通,以及三天内的两个编程经验(CPU频率与计时、windows电源管理API)

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 general

The 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频率没什么关系,这只是时间戳频率。时间戳频率是恒定的。

 

posted @ 2013-01-06 12:05  张拯  阅读(393)  评论(0编辑  收藏  举报