Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
La clasificación de burbujas es más lenta con -O3 que con -O2 con GCC

Hice una implementación de clasificación de burbujas en C y estaba probando su rendimiento cuando noté que el indicador -O3 lo hacía funcionar incluso más lento que si no tuviera ningún indicador. Mientras tanto, -O2 lo estaba haciendo funcionar mucho más rápido como se esperaba.

Sin optimizaciones:

 time ./sort 30000 ./sort 30000 1.82s user 0.00s system 99% cpu 1.816 total

-O2 :

 time ./sort 30000 ./sort 30000 1.00s user 0.00s system 99% cpu 1.005 total

-O3 :

 time ./sort 30000 ./sort 30000 2.01s user 0.00s system 99% cpu 2.007 total

El código:

 #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; i--) { /* will always move at least one element to its rightful place at the end, so can shorten the search by 1 each iteration */ changed = false; for (int x = 0; x < i-1; x++) { if (buf[x] > buf[x+1]) { /* swap */ int tmp = buf[x+1]; buf[x+1] = buf[x]; buf[x] = tmp; changed = true; } } } } int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s <arraysize>\n", argv[0]); return EXIT_FAILURE; } n = atoi(argv[1]); if (n < 1) { fprintf(stderr, "Invalid array size.\n"); return EXIT_FAILURE; } int *buf = malloc(sizeof(int) * n); /* init buffer with random values */ srand(time(NULL)); for (int i = 0; i < n; i++) buf[i] = rand() % n + 1; bubblesort(buf); return EXIT_SUCCESS; }

El lenguaje ensamblador generado para -O2 (de godbolt.org ):

 bubblesort: mov r9d, DWORD PTR n[rip] xor edx, edx xor r10d, r10d .L2: lea r8d, [r9-1] cmp r8d, edx jle .L13 .L5: movsx rax, edx lea rax, [rdi+rax*4] .L4: mov esi, DWORD PTR [rax] mov ecx, DWORD PTR [rax+4] add edx, 1 cmp esi, ecx jle .L2 mov DWORD PTR [rax+4], esi mov r10d, 1 add rax, 4 mov DWORD PTR [rax-4], ecx cmp r8d, edx jg .L4 mov r9d, r8d xor edx, edx xor r10d, r10d lea r8d, [r9-1] cmp r8d, edx jg .L5 .L13: test r10b, r10b jne .L14 .L1: ret .L14: lea eax, [r9-2] cmp r9d, 2 jle .L1 mov r9d, r8d xor edx, edx mov r8d, eax xor r10d, r10d jmp .L5

Y lo mismo para -O3 :

 bubblesort: mov r9d, DWORD PTR n[rip] xor edx, edx xor r10d, r10d .L2: lea r8d, [r9-1] cmp r8d, edx jle .L13 .L5: movsx rax, edx lea rcx, [rdi+rax*4] .L4: movq xmm0, QWORD PTR [rcx] add edx, 1 pshufd xmm2, xmm0, 0xe5 movd esi, xmm0 movd eax, xmm2 pshufd xmm1, xmm0, 225 cmp esi, eax jle .L2 movq QWORD PTR [rcx], xmm1 mov r10d, 1 add rcx, 4 cmp r8d, edx jg .L4 mov r9d, r8d xor edx, edx xor r10d, r10d lea r8d, [r9-1] cmp r8d, edx jg .L5 .L13: test r10b, r10b jne .L14 .L1: ret .L14: lea eax, [r9-2] cmp r9d, 2 jle .L1 mov r9d, r8d xor edx, edx mov r8d, eax xor r10d, r10d jmp .L5

Parece que la única diferencia significativa para mí es el aparente intento de usar SIMD , que parece que debería ser una gran mejora, pero tampoco puedo decir qué diablos está intentando con esas instrucciones pshufd ... ¿es esto solo un intento fallido en SIMD? ¿O tal vez el par de instrucciones adicionales se trata solo de eliminar mi caché de instrucciones?

Los tiempos se realizaron en un AMD Ryzen 5 3600.

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda