注册 登录
编程论坛 C++教室

能告诉我为什么结果与我期望的相反吗?关于 a+=b 和 a=a+b 效率的问题;

jcw08120110 发布于 2014-07-18 16:21, 477 次点击
程序代码:
// 点点滴滴.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdio.h>

#include <iostream>

#include <time.h>

void test1()

{

    int a=1,b=1;
    for(int i=0;i<100000;i++)

        for(int j=0;j<10000;j++)

            a+=b;
}

void test2()
{
    int a=1,b=1;
    for(int i=0;i<100000;i++)

        for(int j=0;j<10000;j++)

            a=a+b;
}
int main(int arg,char ** argv)

{

    clock_t t1,t2;
    float time;

    t1=clock();

    test1();

    t2=clock();

    time=(float)(t2-t1)/CLOCKS_PER_SEC;

    printf("%fs\n",time);

   


    t1=clock();

    test2();

    t2=clock();

    time=(float)(t2-t1)/CLOCKS_PER_SEC;

    printf("%fs\n",time);

   

   

    return 0;

}
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录
3 回复
#2
yuccn2014-07-18 21:07
直接看汇编码,那个指令多,相对就慢咯
#3
zklhp2014-07-19 13:53
按理这两个应该是一样的 至于区别 不同的编译器 不同的参数下 出来的结果肯定不同
#4
zklhp2014-07-19 14:00
特意试了一下 GCC下面 不开编译优化 这两个是一样的
1