以下の二つのプログラムを合体させて計算の時間を測りたいです。
<環境>
- Windows10 64bit
- Ⅽ言語
- gcc(Mingw)
こちらが時間を測るプログラムです。
#include <stdio.h> #include <time.h> int main(void) { clock_t start, end; //int i; start = clock(); printf( "開始時間:%d\n", start ); /* 何かの処理 */ //for( i=0; i<500000000; i++ ); end = clock(); printf( "終了時間:%d\n", end ); printf( "処理時間:%d[ms]\n", end - start ); return 0; }
こちらが計算するプログラムです。
#include<stdio.h> #define N 100000 int main (int argc, char *argv[]) { int i; int *rootBuf; rootBuf = (int *)malloc(N * sizeof(int)); /* Initialize */ for(i=0;i<N;i++){ rootBuf[i] = i; } /* Incriment */ for (i = 0; i < N; i++) { rootBuf[i] = rootBuf[i] + 1; } free(rootBuf); return; }
エラーがでて失敗しているのですが私なりにコードを書いてみました。
何がどのように違うためエラーが起きたのかご指摘していただけないでしょうか。
#include <stdio.h> #include <time.h> #define N 100000 int main(void) { clock_t start, end; int i; int *rootBuf; int A (int argc, char *argv[]); start = clock(); printf( "開始時間:%d\n", start ); rootBuf = (int *)malloc(N * sizeof(int)); /* Initialize */ for(i=0;i<N;i++){ rootBuf[i] = i; } /* Incriment */ for (i = 0; i < N; i++) { rootBuf[i] = rootBuf[i] + 1; } free(rootBuf); } end = clock(); printf( "終了時間:%d\n", end ); printf( "処理時間:%d[ms]\n", end - start ); return 0; }
以下が自分の書いたプログラムのエラーです。
C:\Users\Daito\Desktop\時間を測る計算.c: In function 'main': C:\Users\Daito\Desktop\時間を測る計算.c:9:1: error: stray '\201' in program int i; ^ C:\Users\Daito\Desktop\時間を測る計算.c:9:2: error: stray '@' in program int i; ^ C:\Users\Daito\Desktop\時間を測る計算.c:24:21: warning: implicit declaration of function 'malloc' [-Wimplicit-function-declaration] rootBuf = (int *)malloc(N * sizeof(int)); ^~~~~~ C:\Users\Daito\Desktop\時間を測る計算.c:24:21: warning: incompatible implicit declaration of built-in function 'malloc' C:\Users\Daito\Desktop\時間を測る計算.c:24:21: note: include '<stdlib.h>' or provide a declaration of 'malloc' C:\Users\Daito\Desktop\時間を測る計算.c:36:4: warning: implicit declaration of function 'free' [-Wimplicit-function-declaration] free(rootBuf); ^~~~ C:\Users\Daito\Desktop\時間を測る計算.c:36:4: warning: incompatible implicit declaration of built-in function 'free' C:\Users\Daito\Desktop\時間を測る計算.c:36:4: note: include '<stdlib.h>' or provide a declaration of 'free' C:\Users\Daito\Desktop\時間を測る計算.c:38:1: error: stray '\201' in program } ^ C:\Users\Daito\Desktop\時間を測る計算.c:38:2: error: unknown type name 'p' } ^ C:\Users\Daito\Desktop\時間を測る計算.c:41:3: error: conflicting types for 'end' end = clock(); ^~~ C:\Users\Daito\Desktop\時間を測る計算.c:8:18: note: previous declaration of 'end' was here clock_t start, end; ^~~
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/13 09:06