gpt4 book ai didi

c++ - c++ 编译器是否将多种算法合并为一个循环?

转载 作者:太空宇宙 更新时间:2023-11-04 15:22:23 25 4
gpt4 key购买 nike

假设我们有以下代码:

int main () {
int myints[] = {3,7,2,5,6,4,9};

// using default comparison:
std::cout << "The smallest element is " << *std::min_element(myints,myints+7) << '\n';
std::cout << "The largest element is " << *std::max_element(myints,myints+7) << '\n';
}

编译器会把上面优化成一个循环吗?还是写在一个 for 循环中更好?

最佳答案

这取决于编译器。例如,我的编译器(x86_64 上的g++ 4.7.3)没有,保持两个独立的循环(使用-O3 编译时):

_main:
;=== initialization code omitted ===
jmp L2
L3:
movl (%rax), %edx
cmpl %edx, %ebx
cmovg %edx, %ebx
L2:
addq $4, %rax
cmpq %rbp, %rax
jne L3
;=== output code omitted ===
jmp L8
L6:
movl (%rax), %ecx
cmpl %ecx, (%rdx)
cmovl %rax, %rdx
L8:
addq $4, %rax
cmpq %rbp, %rax
jne L6
;=== output code omitted ===

关于c++ - c++ 编译器是否将多种算法合并为一个循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15862003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com