Ergodyne
April 15th, 2008, 09:17 AM
I have a general question regarding c/c++ regarding what is best to optimize.
Let's say that I have a large array, say of 200.000 elements and I want to look though this array and single out certain elements and save them into another array.
I would then allocate memory for another array of 200.000 elements and then store the elements (or id numbers of these elements) that I find in this array
// Definition of list element
typedef struct ListStruct
{
long lNum;
short nType;
long lRecNum;
double fValue;
} LIST;
// LIST *pList is generated earlier in the code
long iNumberOfElem = 0;
long *piElements = (long *)MemGet( 200001, sizeof(long), NULL );
for( int i = 0; i < 200000; i++ )
{
switch( pList[i].nType )
{
case TYPE_1:
case TYPE_2:
case TYPE_3:
// this operation is very short
break;
case TYPE_4:
//this is the type we are looking for
piElements[++iNumberOfElem] = i;
break;
default:
// Somethin else happens here
break;
}
}
Now another alternative is to first loop though the array and count the elements and then do the memory allocation
typedef struct ListStruct
{
long lNum;
short nType;
long lRecNum;
double fValue;
} LIST;
// LIST *pList is generated earlier in the code
long iNumberOfElem = 0;
// Counting elements in pList array
for( int i=0; i<200000; i++)
{
if( pList[i].nType == TYPE_4 )
{
++iNumberOfElem;
}
}
long *piElements = (long *)MemGet( iNumberOfElem + 1, sizeof(long), NULL );
iNumberOfElem = 0;
for( i = 0; i < 200000; i++ )
{
switch( pList[i].nType )
{
case TYPE_1:
case TYPE_2:
case TYPE_3:
// this operation is very short
break;
case TYPE_4:
//this is the type we are looking for
piElements[++iNumberOfElem] = i;
break;
default:
// Somethin else happens here
break;
}
}
Obviously if you would know the amount of elements in the array it would be easier to now which to choose but say in general. What would be the best to prefer?
Let's say that I have a large array, say of 200.000 elements and I want to look though this array and single out certain elements and save them into another array.
I would then allocate memory for another array of 200.000 elements and then store the elements (or id numbers of these elements) that I find in this array
// Definition of list element
typedef struct ListStruct
{
long lNum;
short nType;
long lRecNum;
double fValue;
} LIST;
// LIST *pList is generated earlier in the code
long iNumberOfElem = 0;
long *piElements = (long *)MemGet( 200001, sizeof(long), NULL );
for( int i = 0; i < 200000; i++ )
{
switch( pList[i].nType )
{
case TYPE_1:
case TYPE_2:
case TYPE_3:
// this operation is very short
break;
case TYPE_4:
//this is the type we are looking for
piElements[++iNumberOfElem] = i;
break;
default:
// Somethin else happens here
break;
}
}
Now another alternative is to first loop though the array and count the elements and then do the memory allocation
typedef struct ListStruct
{
long lNum;
short nType;
long lRecNum;
double fValue;
} LIST;
// LIST *pList is generated earlier in the code
long iNumberOfElem = 0;
// Counting elements in pList array
for( int i=0; i<200000; i++)
{
if( pList[i].nType == TYPE_4 )
{
++iNumberOfElem;
}
}
long *piElements = (long *)MemGet( iNumberOfElem + 1, sizeof(long), NULL );
iNumberOfElem = 0;
for( i = 0; i < 200000; i++ )
{
switch( pList[i].nType )
{
case TYPE_1:
case TYPE_2:
case TYPE_3:
// this operation is very short
break;
case TYPE_4:
//this is the type we are looking for
piElements[++iNumberOfElem] = i;
break;
default:
// Somethin else happens here
break;
}
}
Obviously if you would know the amount of elements in the array it would be easier to now which to choose but say in general. What would be the best to prefer?