| 首页 | 新闻 | 网页 | 设计 | 色彩 | 原创 | 视觉 | 素材 | 动漫 | 酷站 | 策划 | 文案 | 访谈 | 运营 | 编程 | 数据库 | 服务器 | 下载 | 图库 | 
您的位置: 幽幽天空 > 网页 > 编程开发 > Visual C++教程 > 文章正文 用户登录
我做Zanox这三年:
T2click:我爱卡(
T2click:商机热线
推荐:不用手机IC
浅谈CPM、CPC、CP
Java与flash的TCP
简介动画制作过程
AS计算方面的错误
如何用flash做除法
利用文本框制作简

计算cpu速度的小程序           

计算cpu速度的小程序

作者:佚名 来源:不详 更新:2006-8-25 21:05:35 错误报告 我要投稿
以下程序在vc60,console方式下编译运行通过。大家可以试一试,很准的说。
#include <stdio.h>
#include <windows.h>
float measure_clock_speed();
int main(void)
{
    printf("The cpu is running in %f MHz",measure_clock_speed());
    getchar();
    return 1;
}
float measure_clock_speed ()
//-------------------------------------
{
    unsigned long       ticks;
    unsigned long       cycles;
    unsigned long       stamp0,
stamp1;                                        
    unsigned long       freq = 0;
    unsigned long       freq2 =0;
    unsigned long       freq3 =0;
    unsigned long       total;
    unsigned long       tries=0;
    LARGE_INTEGER       t0,t1;                 
    LARGE_INTEGER       count_freq;
    if (!QueryPerformanceFrequency( &count_freq ) )
    {
        return 0.0f;
    }
    unsigned long priority_class     = GetPriorityClass(GetCurrentProcess());

    long          thread_priority    = GetThreadPriority(GetCurrentThread());

    SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
    SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
    do
    {
        tries++;
        freq3 = freq2;
        freq2 = freq;
        QueryPerformanceCounter(&t0);
        t1.LowPart = t0.LowPart;
        t1.HighPart = t0.HighPart;
        while ( (unsigned long)t1.LowPart - (unsigned long)t0.LowPart<50)
        {  
            QueryPerformanceCounter(&t1);
        }
       
        _asm
        {
            rdtsc
            mov stamp0, EAX
        }
        t0.LowPart = t1.LowPart;        // Reset Initial
        t0.HighPart = t1.HighPart;      //   Time
        while ((unsigned long)t1.LowPart-(unsigned long)t0.LowPart<1000 )
        {              
            QueryPerformanceCounter(&t1);
        }
        _asm
        {
            rdtsc
            mov     stamp1, EAX
        }
        cycles = stamp1 - stamp0;
        ticks = (unsigned long) t1.LowPart - (unsigned long) t0.LowPart;   

        ticks = ticks * 100000;            
        ticks = ticks / ( count_freq.LowPart/10 );     
        if ( ticks%count_freq.LowPart > count_freq.LowPart/2 )
        {              
            ticks++;            // Round up if necessary
        }
        freq = cycles/ticks;    // Cycles / us  = MHz
        if ( cycles%ticks > ticks/2 )
        {
            freq++;             // Round up if necessary
        }
        total = ( freq + freq2 + freq3 );
           
    } while (   (tries < 3 ) || (tries < 20) && ((abs(3 * freq -total) > 3)

||
        (abs(3 * freq2-total) > 3) || (abs(3 * freq3-total) > 3)));
       
    if ( total / 3  !=  ( total + 1 ) / 3 )
    {
        total ++;               // Round up if necessary
    }
    // restore the thread priority
    SetPriorityClass(GetCurrentProcess(), priority_class);
    SetThreadPriority(GetCurrentThread(), thread_priority);
    return float(total) / 3.0f;
}
文章录入:skyuu    责任编辑:skyuu 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    发表评论:
    姓名:  评 分: 1分 2分 3分 4分 5分
     
  • 严禁发表危害国家安全、政治、黄色淫秽等内容的评论。
  • 用户需对自己在使用幽幽天空服务过程中的行为承担法律责任。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表机友个人观点,与本网站立场无关。