So I've been attempting to get the runtime for a function in my code using the I'm wondering why my code isn't properly counting the time it takes though, since it is returning 0 seconds for a runtime when it shouldn't. Some possibilities I thought of was that I possibly messed up the 2D array, but if that's the case, shouldn't it return a null pointer?
EDIT: Original code N=500 should produce a runtime of around 1 second from my testing
#include <stdio.h>
#include <sys/time.h>
void matrixMultiplyRow(int N, int matrixA[][N], int matrixB[][N], int final[][N]);
int main(){
int N = 500;
int output[N][N];
int output2[N][N];
int numElements = N*N;
int i, j;
int counter = 1;
//Array 1 fills it with sequential numbers
int experimentA[N][N];
int experimentB[N][N];
for(i=1; i<N+1; i++){
for(j=1; j<N+1; j++){
experimentA[i][j] = counter;
experimentB[i][j] = counter;
counter++;
}
}
struct timeval start_time, stop_time, elapsed_time;
gettimeofday(&start_time,NULL);
matrixMultiplyRow(N, experimentA, experimentB, output);
gettimeofday(&stop_time,NULL);
timersub(&stop_time, &start_time, &elapsed_time);
printf("Total time was %f sec for Row Major.\n", elapsed_time.tv_sec+elapsed_time.tv_usec/1000000.0);
fflush(stdout);
return 1;
}
//Row Major
void matrixMultiplyRow(int N, int matrixA[][N], int matrixB[][N], int final[][N]){
int i, j, k;
for(i=0; i<N; i++){
for(j=0; j<N; j++){
final[i][j] = 0;
for(k=0; k<N; k++){
final[i][j] += matrixA[i][k]*matrixB[k][j];
}
}
}
}