小编ano*_*non的帖子

使用 -O3 进行冒泡排序比使用 GCC 的 -O2 慢

我用 C 语言实现了一个冒泡排序,并在测试其性能时发现该-O3标志使其运行速度甚至比没有标志时还要慢!与此同时-O2,它的运行速度比预期的要快得多。

没有优化:

time ./sort 30000

./sort 30000  1.82s user 0.00s system 99% cpu 1.816 total
Run Code Online (Sandbox Code Playgroud)

-O2

time ./sort 30000

./sort 30000  1.00s user 0.00s system 99% cpu 1.005 total
Run Code Online (Sandbox Code Playgroud)

-O3

time ./sort 30000

./sort 30000  2.01s user 0.00s system 99% cpu 2.007 total
Run Code Online (Sandbox Code Playgroud)

代码:

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

int n;

void bubblesort(int *buf)
{
    bool changed = true;
    for (int i = n; changed == true; …
Run Code Online (Sandbox Code Playgroud)

c gcc x86-64 cpu-architecture compiler-optimization

148
推荐指数
1
解决办法
3万
查看次数

标签 统计

c ×1

compiler-optimization ×1

cpu-architecture ×1

gcc ×1

x86-64 ×1