Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22004

Why is this code 6.5x slower with optimizations enabled?

$
0
0

I wanted to benchmark glibc's strlen function for some reason and found out it apparently performs much slower with optimizations enabled in GCC and I have no idea why.

Here's my code:

#include <time.h>#include <string.h>#include <stdlib.h>#include <stdio.h>int main() {    char *s = calloc(1 << 20, 1);    memset(s, 65, 1000000);    clock_t start = clock();    for (int i = 0; i < 128; ++i) {        s[strlen(s)] = 'A';    }    clock_t end = clock();    printf("%lld\n", (long long)(end - start));    return 0;}

On my machine it outputs:

$ gcc test.c && ./a.out13336$ gcc -O1 test.c && ./a.out199004$ gcc -O2 test.c && ./a.out83415$ gcc -O3 test.c && ./a.out83415

Somehow, enabling optimizations causes it to execute longer.


Viewing all articles
Browse latest Browse all 22004

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>