Skip to content

Instantly share code, notes, and snippets.

@siriusjack
Last active December 23, 2015 23:19
Show Gist options
  • Save siriusjack/6709149 to your computer and use it in GitHub Desktop.
Save siriusjack/6709149 to your computer and use it in GitHub Desktop.
多次元配列 in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/time.h>
double getETime() {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + (double) tv.tv_usec * 1e-6;
}
int main( int argv, char **argc ) {
int NT = 10000;
int NX = 1024;
int NY = 1024;
int x, y, t;
double ts, te;
// version1
ts = getETime();
double **u1, **p1;
u1 = (double**)malloc(sizeof(double*)*(NY+2));
p1 = (double**)malloc(sizeof(double*)*(NY+2));
for (y = 0 ; y < NY+2; y++) {
u1[y] = (double*)malloc(sizeof(double)*(NX+2));
p1[y] = (double*)malloc(sizeof(double)*(NX+2));
}
for (y = 0; y < NY+2; y++) {
for (x = 0; x < NX+2; x++) {
u1[y][x] = x + y;
}
}
for (t = 0; t < NT; t++) {
for (y = 1; y < NY+1; y++) {
for (x = 1; x < NX+1; x++) {
p1[y][x] = u1[y][x] + u1[y+1][x] + u1[y-1][x] + u1[y][x+1] + u1[y][x-1];
}
}
}
te = getETime();
double time1 = te - ts;
printf("1: elapsed time %g\n", time1);
// version2
ts = getETime();
double (*u2)[NX+2];
double (*p2)[NX+2];
u2 = (double(*)[NX+2])malloc(sizeof(double)*(NX+2)*(NY+2));
p2 = (double(*)[NX+2])malloc(sizeof(double)*(NX+2)*(NY+2));
for (y = 0; y < NY+2; y++) {
for (x = 0; x < NX+2; x++) {
u2[y][x] = x + y;
}
}
for (t = 0; t < NT; t++) {
for (y = 1; y < NY+1; y++) {
for (x = 1; x < NX+1; x++) {
p2[y][x] = u2[y][x] + u2[y+1][x] + u2[y-1][x] + u2[y][x+1] + u2[y][x-1];
}
}
}
te = getETime();
double time2 = te - ts;
printf("2: elapsed time %g\n", time2);
// version3
ts = getETime();
double **u3, **p3;
u3 = (double**)malloc(sizeof(double*)*(NY+2));
p3 = (double**)malloc(sizeof(double*)*(NY+2));
for (y = 0 ; y < NY+2; y++) {
u3[y] = (double*)malloc(sizeof(double)*(NX+2));
p3[y] = (double*)malloc(sizeof(double)*(NX+2));
}
for (y = 0; y < NY+2; y++) {
for (x = 0; x < NX+2; x++) {
u3[y][x] = x + y;
}
}
for (t = 0; t < NT; t++) {
for (y = 1; y < NY+1; y++) {
for (x = 1; x < NX+1; x++) {
p3[y][x] = u3[y][x] + u3[y+1][x] + u3[y-1][x] + u3[y][x+1] + u3[y][x-1];
}
}
}
te = getETime();
double time3 = te - ts;
printf("3: elapsed time %g\n", time3);
// version4
ts = getETime();
int ind, ny;
double *u4, *p4;
u4 = (double*)malloc(sizeof(double)*(NY+2)*(NX+2));
p4 = (double*)malloc(sizeof(double)*(NY+2)*(NX+2));
for (y = 0; y < NY+2; y++) {
ny = y * (NX+2);
for (x = 0 ; x < NX+2; x++) {
ind = ny + x;
u4[ind] = x + y;
}
}
int nyu;
for (t = 0; t < NT; t++) {
nyu = NX+2;
for (y = 1; y < NY; y++) {
for (x = 1; x < NX; x++) {
ind = nyu + x;
p4[ind] = u4[ind] + u4[ind+NX+2] + u4[ind-NX-2] + u4[ind+1] + u4[ind-1];
}
nyu += NX+2;
}
}
te = getETime();
double time4 = te - ts;
printf("4: elapsed time %g\n", time4);
// check
nyu = NX+2;
for (y = 1; y < NY; y++) {
for (x = 1; x < NX; x++) {
assert(p1[y][x] == p2[y][x]);
assert(p1[y][x] == p3[y][x]);
assert(p1[y][x] == p4[nyu+x]);
}
nyu += NX+2;
}
return 0;
}
@siriusjack
Copy link
Author

version1: ポインタの配列へのポインタ(メモリはバラバラに確保)
version2: 配列へのポインタ
version3: ポインタの配列へのポインタ(メモリは一括確保)
version4: 一次元配列

@siriusjack
Copy link
Author

1: elapsed time 21.5483
2: elapsed time 20.7368
3: elapsed time 21.0106
4: elapsed time 20.8277

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment