clang和gcc之间的区别 [英] Difference between clang and gcc

查看:1132
本文介绍了clang和gcc之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在不同的项目中都使用了这两个编译器.

I used both of these compilers in different projects.

它们在代码处理和输出生成方面有何不同?例如,gccclang都有-O2选项进行优化.就优化代码而言,它们是否以相同的方式(高级)运行?我做了一点测试,例如,如果我有以下代码:

How they are different in terms of code processing and output generations? For example both gcc and clang has -O2 options for optimization. Are they operating the same way (high level) in terms of optimizing code? I did a little test , for example if I have the following code:

int foo(int num) {
    if(num % 2 == 1)
        return num * num;
    else
        return num * num +1;  
}

以下是带有clang和带有-O2的gcc的输出程序集:

the following are the output assemblies with clang and gcc with -O2:

----gcc 5.3.0-----                              ----clang 3.8.0----
foo(int):                                       foo(int):
        movl    %edi, %edx                              movl    %edi, %eax
        shrl    $31, %edx                               shrl    $31, %eax
        leal    (%rdi,%rdx), %eax                       addl    %edi, %eax
        andl    $1, %eax                                andl    $-2, %eax
        subl    %edx, %eax                              movl    %edi, %ecx
        cmpl    $1, %eax                                subl    %eax, %ecx
        je      .L5                                     imull   %edi, %edi
        imull   %edi, %edi                              cmpl    $1, %ecx
        leal    1(%rdi), %eax                           setne   %al
        ret                                             movzbl  %al, %eax
.L5:                                                    addl    %edi, %eax
        movl    %edi, %eax                              retq
        imull   %edi, %eax
        ret

可以看出,输出具有不同的指令.所以我的问题是,在不同的项目中,其中一个是否比另一个具有优势?

as it can be seen the output has different instructions. So my question is does one of them has advantage over another in different projects?

推荐答案

是.而且不.

这就像询问奥迪汽车是否比梅赛德斯汽车有优势.像他们一样,这两个编译器是两个不同的项目,旨在做同一件事.在某些情况下,gcc会发出更好的代码,而在其他情况下,它将是clang.

This is like asking whether an Audi car has an advantage over a Mercedes car. Like them, the two compilers are two different projects aiming to do the same thing. In some cases, gcc will emit better code, in others it will be clang.

需要知道的时候,必须同时编译两者,然后对其进行度量.

When you need to know, you have to compile your code with both and then measure it.

此处有一个参数,而相关性较小的此处.

There is an argument here and somewhat less related here.

这篇关于clang和gcc之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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