• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

计算cpu速度的小程序

发布: 2007-7-01 20:40 | 作者: admin | 来源: | 查看: 176次 | 进入软件测试论坛讨论

领测软件测试网 以下程序在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;
}

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网