2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Cに複素数型があるんですねー

Last updated at Posted at 2019-05-06
#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

でも遅いみたい。

2
0
11

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?