#include<stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i, j, k;
int i1, j1, k1;
int **array_A;
int **vector_B;
int **vector_C;
int N;
int nb;
N =300;
// nb = 2;
double starttime, endtime, time;
clock_t start, finish;
double duration;
array_A = NULL;
vector_B = NULL;
vector_C = NULL;
array_A = (int **)malloc(N * sizeof(int*));
array_A[0] = (int *)malloc((N*N) * sizeof(int));
for (i = 1; i < N; ++i) array_A[i] = array_A[i - 1] + N;
vector_B = (int **)malloc(N * sizeof(int*));
vector_B[0] = (int *)malloc((N*N) * sizeof(int));
for (i = 1; i < N; ++i) vector_B[i] = vector_B[i - 1] + N;
vector_C = (int **)malloc(N * sizeof(int*));
vector_C[0] = (int *)malloc((N*N) * sizeof(int));
for (i = 1; i < N; ++i) vector_C[i] = vector_C[i - 1] + N;
for (i = 0; i < N; i++)
{
k = i + 1;
for (j = 0; j < N; j++)
{
array_A[i][j] = k++;
}
}
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
vector_B[i][j] = 1;
}
}
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
vector_C[i][j] = 0;
}
}
start = clock();
for (k = 0; k < N; k+=1)
{
for (j = 0; j < N; j+=1)
{
for (i = 0; i < N; i+=1)
{
vector_C[i][j] += array_A[i][k] * vector_B[k][j];
}
}
}
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("%f seconds\n", duration);
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
printf("%4d", array_A[i][j]);
}
printf("\n");
}
printf("\n");
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
printf("%4d", vector_C[i][j]);
}
printf("\n");
}
fflush(NULL);
free(array_A[0]);
free(array_A);
array_A = NULL;
free(vector_B);
vector_B = NULL;
free(vector_C);
vector_C = NULL;
return 0;
}
2.分块
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i, j, k;
int i1, j1, k1;
int **array_A;
int **vector_B;
int **vector_C;
int N;
int nb;
N = 100;
nb = 2;
double starttime, endtime, time;
clock_t start, finish;
double duration;
array_A = NULL;
vector_B = NULL;
vector_C = NULL;
array_A = (int **)malloc(N * sizeof(int*));
array_A[0] = (int *)malloc((N*N) * sizeof(int));
for (i = 1; i < N; ++i) array_A[i] = array_A[i - 1] + N;
vector_B = (int **)malloc(N * sizeof(int*));
vector_B[0] = (int *)malloc((N*N) * sizeof(int));
for (i = 1; i < N; ++i) vector_B[i] = vector_B[i - 1] + N;
vector_C = (int **)malloc(N * sizeof(int*));
vector_C[0] = (int *)malloc((N*N) * sizeof(int));
for (i = 1; i < N; ++i) vector_C[i] = vector_C[i - 1] + N;
for (i = 0; i < N; i++)
{
k = i + 1;
for (j = 0; j < N; j++)
{
array_A[i][j] = k++;
}
}
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
vector_B[i][j] = 1;
}
}
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
vector_C[i][j] = 0;
}
}
start = clock();
for (i = 0; i < N; i+=nb)
{
for (j = 0; j < N; j+=nb)
{
for (k = 0; k < N; k+=nb)
{
for (i1 = i; i1 < i+nb; i1++)
{
for (j1 = j; j1 < j+nb; j1++)
{
for (k1 = k; k1 < k+nb; k1++)
{
vector_C[i1][j1] += array_A[i1][k1] *
vector_B[k1][j1];
}
}
}
}
}
}
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("%f seconds\n", duration);
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
printf("%4d", array_A[i][j]);
}
printf("\n");
}
printf("\n");
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
printf("%4d", vector_C[i][j]);
}
printf("\n");
}
fflush(NULL);
free(array_A[0]);
free(array_A);
array_A = NULL;
free(vector_B);
vector_B = NULL;
free(vector_C);
vector_C = NULL;
return 0;
}
评论0