468,458 Members | 1,770 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,458 developers. It's quick & easy.

Performance Considerations in 3D arrays

Im a relatively new coder and am still learning the best way to do thigs
- and I have always been told the best way to learn is by writing
testing code and measuring, Could anyone shed some light on why my
'MyArray[,,]' test is so much faster? Id really like to know!

What is the best met

Cheers Guys
Jon Rea

Application Output

WindowsXP:
Sum: 251437500.000000
Sum: 251437500.000000
Array3D test Took 30 seconds
Sum: 251437500.000000
Sum: 251437500.000000
MyArray[] test Took 30 seconds
Sum: 251437500.000000
Sum: 251437500.000000
MyArray[] (V2) test Took 29 seconds
Sum: 251437500.000000
Sum: 251437500.000000
MyArray[,,] test Took 11 seconds

Linux (RH9):
Sum: 251437500.000000
Sum: 251437500.000000
Array3D test Took 20 seconds
Sum: 251437500.000000
Sum: 251437500.000000
MyArray[] test Took 20 seconds
Sum: 251437500.000000
Sum: 251437500.000000
MyArray[] (V2) test Took 22 seconds
Sum: 251437500.000000
Sum: 251437500.000000
MyArray[,,] test Took 6 seconds

Application Code
#include <iostream>
#include <stdio.h>
#include <tchar.h>
#include <time.h>

#include "tntjama/tnt_array3d.h"

using namespace TNT;

int _tmain(int argc, _TCHAR* argv[])
{
int count1 = 15;
int count2 = 100;

int M = 150;
int N = 150;
int P = 150;

time_t starttime = time( NULL );
for( int r = 0; r < count1; r++ )
{
Array3D< double A(M,N,P);

for (int i=0; i < M; i++)
for (int j=0; j < N; j++)
for (int k=0; k < P; k++)
A[i][j][k] = (double)k; /* initalize array values */

for( int q = 0; q < count2; q++ )
{
double sum = 0.0;
for (int i=0; i < M; i++)
for (int j=0; j < N; j++)
for (int k=0; k < P; k++)
sum += A[i][j][k]; /* calc sum */
if( r == 0 && (q == 0 || q == 15) ) printf("Sum: %lf\n",sum);
}
}
printf("Array3D test Took %d seconds\n",(int)(time(NULL) - starttime));

starttime = time( NULL );
int T = M*M;
for( int r = 0; r < count1; r++ )
{
double *A = new double[M*N*P];

for (int i=0; i < M; i++)
for (int j=0; j < N; j++)
for (int k=0; k < P; k++)
A[(T*i)+(N*j)+k] = (double)k; /* initalize array values */

for( int q = 0; q < count2; q++ )
{
double sum = 0.0;
for (int i=0; i < M; i++)
for (int j=0; j < N; j++)
for (int k=0; k < P; k++)
sum += A[(T*i)+(N*j)+k]; /* calc sum */
if( r == 0 && (q == 0 || q == 15) ) printf("Sum: %lf\n",sum);
}

delete[] A;
}
printf("MyArray[] test Took %d seconds\n",(int)(time(NULL) - starttime));

starttime = time( NULL );
T = M*M;
int indexRoot1, indexRoot2;
for( int r = 0; r < count1; r++ )
{
double *A = new double[M*N*P];

for (int i=0; i < M; i++)
{
indexRoot1 = (T*i);
for (int j=0; j < N; j++)
{
indexRoot2 = indexRoot1 + (N*j);
for (int k=0; k < P; k++)
{
A[indexRoot2+k] = (double)k; /* initalize array values */
}
}
}

for( int q = 0; q < count2; q++ )
{
double sum = 0.0;
int indexRoot1, indexRoot2;
for (int i=0; i < M; i++)
{
indexRoot1 = (T*i);
for (int j=0; j < N; j++)
{
indexRoot2 = indexRoot1 + (N*j);
for (int k=0; k < P; k++)
{
sum += A[indexRoot2+k]; /* calc sum */
}
}
}
if( r == 0 && (q == 0 || q == 15) ) printf("Sum: %lf\n",sum);
}

delete[] A;
}
printf("MyArray[] (V2) test Took %d seconds\n",(int)(time(NULL) -
starttime));

starttime = time( NULL );
for( int r = 0; r < count1; r++ )
{
double *A = new double[M,N,P];

for (int i=0; i < M; i++)
for (int j=0; j < N; j++)
for (int k=0; k < P; k++)
A[i,j,k] = (double)k; /* initalize array values */

for( int q = 0; q < count2; q++ )
{
double sum = 0.0;
for (int i=0; i < M; i++)
for (int j=0; j < N; j++)
for (int k=0; k < P; k++)
sum += A[i,j,k]; /* calc sum */
if( r == 0 && (q == 0 || q == 15) ) printf("Sum: %lf\n",sum);
}

delete[] A;
}
printf("MyArray[,,] test Took %d seconds\n",(int)(time(NULL) - starttime));

return 0;
}
Jul 9 '06 #1
1 1024
Jon Rea wrote, On 9.7.2006 15:56:
Im a relatively new coder and am still learning the best way to do thigs
- and I have always been told the best way to learn is by writing
testing code and measuring, Could anyone shed some light on why my
'MyArray[,,]' test is so much faster? Id really like to know!
C++ in nost Pascal. double[a,b,c] doesn't do what you think it does.

--
VH
Jul 10 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Dave Theese | last post: by
8 posts views Thread by Sebastian Werner | last post: by
115 posts views Thread by Mark Shelor | last post: by
37 posts views Thread by jortizclaver | last post: by
11 posts views Thread by ZenRhapsody | last post: by
2 posts views Thread by Jon Rea | last post: by
2 posts views Thread by Martien van Wanrooij | last post: by
1 post views Thread by subramanian100in | last post: by
13 posts views Thread by atlaste | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.