// Program to take the sum of the members of a structure to another array
#include <stdio.h>
#include <time.h>
struct Vector {
double *a, *b, *c, *d, *e, *f, *g, *h;
};
int main()
{
int i, k, n;
Vector vec;
double *r;
printf("Array size Elapsed time [sec] \n");
for (int k=1; k<=20; k++) {
// Array size
int n = k * 2000000;
// Allocation
r = new double[n];
vec.a = new double[n]; vec.b = new double[n];
vec.c = new double[n]; vec.d = new double[n];
vec.e = new double[n]; vec.f = new double[n];
vec.g = new double[n]; vec.h = new double[n];
// Initialization
for (i=0; i<n; i++) {
vec.a[i] = 1.0;
vec.b[i] = 1.0;
vec.c[i] = 1.0;
vec.d[i] = 1.0;
vec.e[i] = 1.0;
vec.f[i] = 1.0;
vec.g[i] = 1.0;
vec.h[i] = 1.0;
}
// Start time
clock_t time0 = clock();
// Main calculation: find the sum of all members of vec
for (i=0; i<n; i++)
r[i] = vec.a[i] + vec.b[i] + vec.c[i] + vec.d[i]
+ vec.e[i] + vec.f[i] + vec.g[i] + vec.h[i];
// Finish time
clock_t time1 = clock();
// Output time
double time = (double)(time1-time0)/CLOCKS_PER_SEC;
printf("%11d %15.7f\n",n, time);
// Deallocation
delete[] r;
delete[] vec.a; delete[] vec.b;
delete[] vec.c; delete[] vec.d;
delete[] vec.e; delete[] vec.f;
delete[] vec.g; delete[] vec.h;
}
return 0;
}
|
// Program to take the sum of the members of a structure to another array
#include <stdio.h>
#include <time.h>
struct Vector {
double a, b, c, d, e, f, g, h;
};
int main()
{
int i, k, n;
Vector *vec;
double *r;
printf("Array size Elapsed time [sec] \n");
for (int k=1; k<=20; k++) {
// Array size
int n = k * 2000000;
// Allocation
r = new double[n];
vec = new Vector[n];
// Initialization
for (i=0; i<n; i++) {
vec[i].a = 1.0;
vec[i].b = 1.0;
vec[i].c = 1.0;
vec[i].d = 1.0;
vec[i].e = 1.0;
vec[i].f = 1.0;
vec[i].g = 1.0;
vec[i].h = 1.0;
}
// Start time
clock_t time0 = clock();
// Main calculation: find the sum of all members of vec
for (i=0; i<n; i++)
r[i] = vec[i].a + vec[i].b + vec[i].c + vec[i].d
+ vec[i].e + vec[i].f + vec[i].g + vec[i].h;
// Finish time
clock_t time1 = clock();
// Output time
double time = (double)(time1-time0)/CLOCKS_PER_SEC;
printf("%11d %15.7f\n",n, time);
// Deallocation
delete[] r;
delete[] vec;
}
return 0;
}
|