実行時間を計ってみた

実験には、こんなコードを使ってみた。

// Simple timer example

#include "mbed.h"

Serial pc(USBTX, USBRX); // tx, rx

Timer t;

int main() {
    int i;
    double x,y;
   
    t.start();
    pc.printf("Hello World!\n");
    t.stop();
    pc.printf("The time taken was %f seconds\n", t.read());
    pc.printf("The time taken was %d ms\n", t.read_ms());
    t.reset();
    t.start();

    x=1.0;
    y=3.14;
   
    for(i=0;i<1000;i++){
        x=x*y;
    }

    t.stop();
    pc.printf("The time was %d ms %d us\n", t.read_ms(), t.read_us());

    t.reset();
    t.start();

    x=1.0;
    y=3.14;
   
    for(i=0;i<1000;i++){
        x=sin(y*i);
    }

    t.stop();
    pc.printf("The time was %d ms %d us\n", t.read_ms(), t.read_us());
}

ターミナルの結果は

Hello World!
The time taken was 0.011593 seconds
The time taken was 11 ms
The time was 0 ms 42 us
The time was 32 ms 32951 us

浮動少数の掛け算のとこは、何か端折られた感じがするけど、

三角関数は予測範囲内と言った感じ。

コンパイラの最適化を細かく制御できないと安心はできないかなぁ。

 


0 comments

You need to log in to post a comment