写了个程序cpu使用率太高了?在写代码的时候加法快还是乘法快还是都一样
#include stdio.h
#include windows.h
#include stdlib.h
int main()
{
tDWORD time;
tint h, m, s, x;
twhile(①)
t{
tttime=GetTickCount(); //返回的是机子运行了多少毫秒
ttx = (int)time/①⓪⓪⓪; //转换为秒
tth = (x - (x%③⑥⓪⓪))/③⑥⓪⓪; // 这里是计算小时,下面分别是分,秒
ttm = ((x%③⑥⓪⓪) -((x%③⑥⓪⓪)%⑥⓪))/⑥⓪;
tts = x -(( ③⑥⓪⓪ * h) + (⑥⓪ * m));
ttprintf("文龙为您报时,您的电脑已运行:n");
ttprintf("================================================================================");
ttprintf("nnnnntttt%d : %d : %d
① · 反正是精度①秒的时钟,加个⑨⑨⑨毫秒的sleep
② · 别用这种傻循环好吗,系统有定时器(timer)这种东西
\", \"extras\": \"\", \"created_time\": ①④⑧⑧⑧⑥⑨⑧③⑤ · \"type\": \"answer
写成死循环……不高就有鬼了
而且你精度只有秒
直接Sleep(①⓪⓪⓪);就好\", \"extras\": \"\", \"created_time\": ①④⑧⑧⑥③⑥⑨⑦⓪ · \"type\": \"answer
while(①)永远是真,循环不会结束,CPU再强也顶不住。。。\", \"extras\": \"\", \"created_time\": ①④⑧⑧⑧⑤⑧⑧②④ · \"type\": \"answer
如果写的是C语言的话,真的不用纠结这个问题,按照可读性最好的来写。
编译器比大部分程序员都聪明(在优化指令上)。手写的汇编大部分赶不上编译器生成的指令。
比如像这样的①段
int main(){ int sum; sum = ①⓪; sum *= ③; return sum;}gcc -O⓪ 编译出来以后是这个样子的
Dump of assembler code for function main: ⓪x⓪⓪⓪⓪⓪⓪⓪⓪⓪⓪④⓪⓪④ec :push rbp ⓪x⓪⓪⓪⓪⓪⓪⓪⓪⓪⓪④⓪⓪④ed :mov rbp,rsp ⓪x⓪⓪⓪⓪⓪⓪⓪⓪⓪⓪④⓪⓪④f⓪ :mov DWORD PTR [rbp-⓪x④],⓪xa ⓪x⓪⓪⓪⓪⓪⓪⓪⓪⓪⓪④⓪⓪④f⑦ :mov edx,DWORD PTR [rbp-⓪x④] ⓪x⓪⓪⓪⓪⓪⓪⓪⓪⓪⓪④⓪⓪④fa :mov eax,edx ⓪x⓪⓪⓪⓪⓪⓪⓪⓪⓪⓪④⓪⓪④fc :add eax,eax ⓪x⓪⓪⓪⓪⓪⓪⓪⓪⓪⓪④⓪⓪④fe :add eax,edx ⓪x⓪⓪⓪⓪⓪⓪⓪⓪⓪⓪④⓪⓪⑤⓪⓪ :mov DWORD PTR [rbp-⓪x④],eax ⓪x⓪⓪⓪⓪⓪⓪⓪⓪⓪⓪④⓪⓪⑤⓪③ :mov eax,DWORD PTR [rbp-⓪x④] ⓪x⓪⓪⓪⓪⓪⓪⓪⓪⓪⓪④⓪⓪⑤⓪⑥ :pop rbp ⓪x⓪⓪⓪⓪⓪⓪⓪⓪⓪⓪④⓪⓪⑤⓪⑦ :ret End of assembler dump.
sum *= ③; 被拆成了①条mov和两条add,没有用乘法。(这还是-O⓪)
如果是sum *= ②; 的话,就是这个样子
Dump of assembler code for function main: ⓪x⓪⓪⓪⓪⓪⓪⓪⓪⓪⓪④⓪⓪④ec :push rbp ⓪x⓪⓪⓪⓪⓪⓪⓪⓪⓪⓪④⓪⓪④ed :mov rbp,rsp ⓪x⓪⓪⓪⓪⓪⓪⓪⓪⓪⓪④⓪⓪④f⓪ :mov DWORD PTR [rbp-⓪x④],⓪xa ⓪x⓪⓪⓪⓪⓪⓪⓪⓪⓪⓪④⓪⓪④f⑦ :shl DWORD PTR [rbp-⓪x④],① ⓪x⓪⓪⓪⓪⓪⓪⓪⓪⓪⓪④⓪⓪④fa :mov eax,DWORD PTR [rbp-⓪x④] ⓪x⓪⓪⓪⓪⓪⓪⓪⓪⓪⓪④⓪⓪④fd :pop rbp ⓪x⓪⓪⓪⓪⓪⓪⓪⓪⓪⓪④⓪⓪④fe :ret End of assembler dump.看那行shl,被当作 sum
- 5星
- 4星
- 3星
- 2星
- 1星
- 暂无评论信息