C 言語メモ

2021年1月9日

計算時間の計測

#include <stdio.h>
#include <time.h>


int main(int argc, char *argv[])
{
    clock_t start_time;
    clock_t end_time;
    double elapsed_time;

    start_time = clock();

    /* 処理 */

    end_time = clock();

    elapsed_time = (end_time - start_time)/(double)CLOCKS_PER_SEC;
    printf("Elapsed Time: %f s\n", elapsed_time);

    return 0;
}

乱数

#include <stdlib.h>


int randi(int rmin, int rmax)
{
    return (int)(rmin + (rand()/(double)RAND_MAX)*(rmax - rmin));
}


double randf(double rmin, double rmax)
{
    return rmin + (rand()/(double)RAND_MAX)*(rmax - rmin);
}