[cfe-dev] One little test, comparing clang and gcc
Giangiacomo Mariotti
gg.mariotti at gmail.com
Thu Oct 29 17:38:43 PDT 2009
On Thu, Oct 29, 2009 at 8:17 PM, Daniel Dunbar <daniel at zuster.org> wrote:
> I don't know the answers to these questions without digging in to your
> example -- there is a lot of information here which I find hard to
> read. I think you are asking about the time difference between
> clang_02 and gcc_02, this is probably due to some missed optimization
> which of course we always would like to fix. However, you'll have to
> actually dig in to the example to find out what is going on. If you
> can identify a small test case where clang clearly isn't optimizing
> something it should, please file a bugzilla for it
> (http://llvm.org/bugs).
>
Let's make the example code much shorter:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAXN 100000
int x[MAXN];
int startn = 5000;
int n;
#define T(s, n)\
do {\
typeof(s) s__ = s;\
typeof(n) n__ = n;\
printf("%s (n=%d)\n", s__, n__);\
} while (0)
#define TRIALS 5
#define M(op) do { \
printf(" %-26s", #op); \
k = 0; \
timesum = 0; \
for (ex = 0; ex < TRIALS; ex++) { \
start = clock(); \
for (i = 1; i <= n; i++) { \
fi = (float) i; \
for (j = 1; j <= n; j++) { \
op; \
} \
} \
t = clock()-start; \
printf("%8d", t); \
timesum += t; \
} \
nans = 1e9 * timesum / ((double) \
n*n * TRIALS * CLOCKS_PER_SEC); \
printf("%8.0f\n", nans); } while (0)
int main(void)
{
int i, j, k;
float fi;
int t, ex, timesum, start;
double nans;
for (i = 0; i < n; i++)
x[i] = rand();
n = startn;
printf("C Time Cost Model, n=%d\n", n);
M({});
return 0;
}
Here, if we look at the instruction that is timed and we ask ourself,
what is that instruction doing? The answer is simply: absolutely
nothing.
Now these are the results for the code compiled as explained on the
previous mail:
->time_tests_001_gcc_O2
C Time Cost Model, n=5000
{} 0 0 0 0 0 0
->time_tests_001_clang_O2
C Time Cost Model, n=5000
{} 10000 20000 20000 20000 20000 1
So the point is, at optimization level -O2, gcc compiles out code that
does nothing, while clang optimizes it, but it still leaves the code
there.
(Notice that I haven't inspected the object file, so I may be wrong. I
wanted to post this here, before doing that)
Giangiacomo Mariotti
More information about the cfe-dev
mailing list