率++一个,一++ A = A + 1和一个+ = 1中的执行效率方面中C.Assume GCC是编译器 [英] rate ++a,a++,a=a+1 and a+=1 in terms of execution efficiency in C.Assume gcc to be the compiler

查看:126
本文介绍了率++一个,一++ A = A + 1和一个+ = 1中的执行效率方面中C.Assume GCC是编译器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/24901/is-there-a-performance-difference-between-i-and-i-in-c\">Is有性能差异之间我++和++ i的C ++?


在下面的使用条款,请评价在C的执行时间
在一些采访我被问其中应我的这些变化之间的使用和为什么。

  A ++
++一个
A = A + 1
一个+ = 1


解决方案

下面是 G ++ -S 生产:

 无效irrelevant_low_level_worries()
{
    INT A = 0;
// MOVL $ 0 -4(%EBP)    一个++;
//包括-4(%EBP)    ++一个;
//包括-4(%EBP)    A = A + 1;
//包括-4(%EBP)    一个+ = 1;
//包括-4(%EBP)
}

所以,即使没有任何优化开关,所有四个语句编译完全相同的机器code。

Possible Duplicate:
Is there a performance difference between i++ and ++i in C++?

In terms of usage of the following, please rate in terms of execution time in C. In some interviews i was asked which shall i use among these variations and why.

a++
++a
a=a+1
a+=1

解决方案

Here is what g++ -S produces:

void irrelevant_low_level_worries()
{
    int a = 0;
//  movl    $0, -4(%ebp)

    a++;
//  incl    -4(%ebp)

    ++a;
//  incl    -4(%ebp)

    a = a + 1;
//  incl    -4(%ebp)

    a += 1;
//  incl    -4(%ebp)
}

So even without any optimizer switches, all four statements compile to the exact same machine code.

这篇关于率++一个,一++ A = A + 1和一个+ = 1中的执行效率方面中C.Assume GCC是编译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆