搜索
查看: 2894|回复: 34
打印 上一主题 下一主题

小调查:运行计时程序,测试电脑运行速度

[复制链接]
楼主
发表于 2017-8-8 14:55:47 | 显示全部楼层
[mw_shl_code=c,true]#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main()
{
    int i = 0;
    unsigned s = GetTickCount();
    loop:;
    if(1000000000 - (i++)) goto loop;
    printf("%ums",GetTickCount() - s);
    system("@pause > nul");
    return 0;
}[/mw_shl_code]
沙发
发表于 2017-8-8 14:58:39 | 显示全部楼层
上面的代码测得的时间更加准确更加精确:
1.省去了类型转化浪费的时间
2.除去了代价高的除法运算
3.使用效率更高的goto语句代替了while循环
4.直接调用系统api 效率更快
板凳
发表于 2017-8-8 15:01:04 | 显示全部楼层
赵林 发表于 2017-8-8 14:44
初学C,你的电脑有那么快?
4499APPLE,我怀疑你是故意捣乱

是这样子的 我把这个程序放在了windows xp上面运行(XP运行程序的速度你知道的)(加上CPU主频比较高)
地板
发表于 2017-8-8 15:08:09 | 显示全部楼层
本帖最后由 初学C 于 2017-8-8 15:15 编辑

不过还有一种方法是结果更加精确:使用寄存器变量
[mw_shl_code=c,true]#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main()
{
    register int i = 0;
    register unsigned s = GetTickCount();
    loop:;
    if(1000000000 - (i++)) goto loop;
    printf("%ums",GetTickCount() - s);
    system("@pause > nul");
    return 0;
}[/mw_shl_code]
这段代码我的xp上运行结果是245ms(对不起打错了是545ms)
在我的win8上运行结果是1188ms

5#
发表于 2017-8-8 18:36:28 | 显示全部楼层
赵林 发表于 2017-8-8 17:53
另外我的Win10运行结果为2814ms,用我的方法改完之后是2598ms,节省了200ms。
但是用goto代替while并用Get ...


这个速度肯定快一些(100000000 - (i++))
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表