哪个操作在c中更快?为什么?加法还是减法? [英] Which operation is faster in c and why ? Addition or Subtraction ?

查看:340
本文介绍了哪个操作在c中更快?为什么?加法还是减法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑,因为这个问题在接受采访时被问到了。

我认为加法更快但不知道原因。

I am confused as this question was asked to me in an interview.
I think addition is faster but dont know the reason.

推荐答案

现在你不在采访中。计时或使用CPU参考;这些引用始终提供每个命令所采用的总线周期信息。 (这些问题应该问一些特定的CPU;但是,很难想象会有什么不同。)



严格来说,问题是不正确的。 指令集架构通常(不是全部)对于不同的命令具有不同的时序。他们怎么会有所不同?不同的类型(浮点计算可以在CPU的不同部分进行,称为FPU)有很大的不同,但操作的源和目标可以产生相当大的差异(内存,寄存器)。



-SA
Now you are not at the interview. Time it or use the CPU reference; such references always provide information of bus cycles taken by each command. (Such questions should be asked about some particular CPU; however, it's hard to imagine that there can be any difference).

Strictly speaking, the question is incorrect. The instruction-set architectures typically (not all of them) have different timing for different commands like that. How can they be different? Different types (floating-point calculation can be done in a different part of CPU, called FPU) makes big difference, but also sources and targets of the operations can make considerable difference (memory, register).

—SA


我想,这个问题只是让你迷惑。首先,不是C或C ++语言规定了加法或减法的时间,而是处理器。对于二者来说,今天的大多数处理器的实现方式是加法和减法基本上都是用相同的资源完成的。如果执行时间有任何差异,则它是从属的。所以你的回答应该是:出于所有实际目的,它们以相同的速度运行。但请注意:乘法和除法也是如此。通常除法速度要快得多。
I guess, this question was just asked to confuse you. For one, it is not the C or C++ language that dictates the time for an addition or subtraction, but the processor. For two, most of today's processors are implemented in a way that addition and subtraction are basically done with the same resources. If there is any difference in execution time, it is subordinate. So your your answer should have been: For all practical purposes they run with the same speed. But note: The same is NOT true for multiplication and division. Usually division is a lot slower.


其他解决方案都是正确的,主要取决于处理器的实现。



我决定尝试一下。下面是一个N = 10000000的测试,加法花费.035s,减法花费.044s。



编辑:此编辑器转换>到& gt和&到& amp;



The other solutions are correct, it's mainly up to the implementation of the processor.

I decided to try it. Below is a test for N = 10000000, addition took .035s and subtraction took .044s.

this editor converts > to &gt and & to &

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void add(int *a, int b)
{
    *a = *a + b;
}

void sub(int *a, int b)
{
    *a = *a - b;
}

void runAddTest(int n)
{
    int i=0;
    while(n-->0)
    {
        add(&i,1);
    //printf("%d, ",i);
    }
    //printf("\n");
}

void runSubTest(int n)
{
    int i=n;
    while(n-->;0)
    {
        sub(&i,1);
        //printf("%d, ",i);
    }
    //printf("\n");
}

int main()
{
    int N = 10000000;
    printf("Running, please wait...\n");

    clock_t t;
    t=clock();
    runAddTest(N);
    t=clock() -t;

    clock_t t2=clock();
    runSubTest(N);
    t2=clock() -t2;

    printf("Addition: Time %f seconds.\n",((float)t)/CLOCKS_PER_SEC);
    printf("Subtraction: Time %f seconds.\n",((float)t2)/CLOCKS_PER_SEC);

    getchar();
    return 0;
}


这篇关于哪个操作在c中更快?为什么?加法还是减法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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