游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1640|回复: 2

请教一个初级问题。,望DX们帮忙

[复制链接]

6

主题

444

帖子

457

积分

中级会员

Rank: 3Rank: 3

积分
457
发表于 2004-2-19 09:29:00 | 显示全部楼层 |阅读模式
这应该属于初级问题了。
就是:在程序中如何获得系统信息:比如CPU频率。内存大小等等?
望大家不吝赐教,谢谢。

1万

主题

1万

帖子

2万

积分

管理员

中级会员

Rank: 9Rank: 9Rank: 9

积分
20505
发表于 2004-2-19 09:55:00 | 显示全部楼层

Re:请教一个初级问题。,望DX们帮忙

int GetCPUSpeed( void )
{
        DWORD                        dwFeatures;
        DWORD                        dwStartTime;
        DWORD                        dwStopTime;
        __int64                        i64StartTicks;
        __int64                        i64EndTicks;
        __int64                        i64TotalTicks;
        int                                iSpeed;


        // Get the capabilities of the cpu.
        dwFeatures = GetCPUCaps();

        // The rdtsc instruction must be available.
        if ( !( dwFeatures & CPU_FEATURE_RDTSC ) )
                return -1;


        // Get the start time.
        dwStartTime = timeGetTime();

        //
        // Wait for a new millisecond to start.
        //
        do
        {
                dwStopTime = timeGetTime();
        }
        while ( ( dwStopTime - dwStartTime ) == 0 );

        // Use the new start time.
        dwStartTime = dwStopTime;


        //
        // Get the start tick count.
        //
        __asm
        {
                //
                // Save registers.
                //
                push        eax
                        push        ebx
                        push        edx

                        //
                        // Get the tick count.
                        //
                        rdtsc
                        lea                ebx, i64StartTicks
                        mov                [ebx], eax
                        mov                [ebx + 4], edx

                        //
                        // Restore registers.
                        //
                        pop                edx
                        pop                ebx
                        pop                eax
        }


        //
        // Loop till time? up.
        //
        while ( true )
        {               
                // If one second is over ...
                if ( ( timeGetTime() - dwStartTime ) >= 1000 )
                {
                        //
                        // ... get the end tick count.
                        //
                        __asm
                        {
                                //
                                // Save registers.
                                //
                                push        eax
                                        push        ebx
                                        push        edx

                                        //
                                        // Get the tick count.
                                        //
                                        rdtsc
                                        lea                ebx, i64EndTicks
                                        mov                [ebx], eax
                                        mov                [ebx + 4], edx

                                        //
                                        // Restore registers.
                                        //
                                        pop                edx
                                        pop                ebx
                                        pop                eax
                        }

                        break;       
                }       
        }

        // Calculate the difference in clock ticks.
        i64TotalTicks = i64EndTicks - i64StartTicks;

        // Calculate the speed in Mhz.
        iSpeed = ( int ) ( i64TotalTicks / 1000000 );               

        // Return the speed in Mhz.
        return iSpeed;                       
}

/*
* Description:                Retrieves the cpu features that the processor supports.
*
* Parameters:                None.
*
* Return value:        A combination of the following flags:
*
*                                        CPU_FEATURE_MMX   - The MMX instruction set is available.
*                                        CPU_FEATURE_RDTSC - The rdtsc instruction is available.
*                                        CPU_FEATURE_3DNOW - The 3DNow! instruction set is available.
*
*/
DWORD GetCPUCaps( void )
{
        SYSTEM_INFO                si;
        DWORD                        dwFeatures;


        // Assume no features are present.
        dwFeatures = 0;

        // Get the system information.
        GetSystemInfo(&si);

        // If this is at least a pentium or compatibel ...
        if ( si.dwProcessorType != PROCESSOR_INTEL_386 &&
                si.dwProcessorType != PROCESSOR_INTEL_486)
        {
                //
                // ... get the features.
                //
                __asm
                {
                        //
                        // Save registers.
                        //
                        push        eax
                                push        ebx
                                push        ecx
                                push        edx

                                //
                                // Get the features.
                                //
                                mov                eax, 1
                                cpuid
                                mov                dwFeatures, edx

                                //
                                // Restore registers.
                                //
                                pop                edx
                                pop                ecx
                                pop                ebx
                                pop                eax
                }
        }

        // Return the features.
        return dwFeatures;
}

6

主题

444

帖子

457

积分

中级会员

Rank: 3Rank: 3

积分
457
 楼主| 发表于 2004-2-19 10:33:00 | 显示全部楼层

Re:请教一个初级问题。,望DX们帮忙

也就是说是使用rdtsc计算1秒的cycle数然后得出结果……这方法我知道。
除此之外别无他法?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-5-14 19:48

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表