#include <stdio.h>
#include <complex.h>
#include <time.h>
typedef struct {
double re, im;
} C;
#define N 100000000
int main()
{
clock_t start;
start = clock();
for (int i = 0; i < N; i++) {
double complex a = 1.0 + 3.0*I;
double complex b = 1.0 - 4.0*I;
double complex c = a*b;
}
printf("%f\n", 1.0*(clock() - start)/CLOCKS_PER_SEC);
start = clock();
for (int i = 0; i < N; i++) {
C a = { 1.0, 3.0 };
C b = { 1.0, -4.0 };
C c = {
.re = a.re*b.re - a.im*b.im,
.im = a.re*b.im + a.im*b.re
};
}
printf("%f\n", 1.0*(clock() - start)/CLOCKS_PER_SEC);
return 0;
}
結果
$ cc --version
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$ cc a.c
$ ./a.out
0.489901
0.298011
$ !!
./a.out
0.513582
0.306521
$ !!
./a.out
0.489888
0.296904
$ !!
./a.out
0.521009
0.303007
yoshioka-no-MacBook-Pro:tmp ynaoto$ !!
./a.out
0.486675
0.308325
でも遅いみたい。