Need help on sorting array of objects
Posted: Thu Apr 28, 2022 2:50 pm
Hi,
I’m new to X# and facing few performance issues the code written in xSharp.
We have an array of 1500 objects. We are looping through each element of this array for some calculation and assigning it’s value to a list.
In the calculation part we have another method that is using same array to loop through and calculating sum based on a condition. This is resulting in nested loops (1500 x 1500) and causing too much delay(about 9 mins) in processing this method.
Could you please suggest me the optimal way to filter the array based on required condition instead of scanning through each element of entire array.
CLASS A
aArray as Array //this is array of objects
METHOD Method1
nLen := aLen(aArray)
for n := 1 upTo nLen
currentElelment := aArray[n]
//some calculation
nSum := CalculateSum(currentDate, DueDate)
//assigning cuurentElement to list
Next
METHOD CalculateSum(currentDate, DueDate)
cbCondition := {|o| o:validDate(currentDate) .and. DateTime2cDate(o:inRangeDate(currentDate)) <= DueDate}
if (iRecs:= aArray:count) > 0
for iRec := 0 upTo iRecs //looping through same array of 1500 objects
if (oRec := aArray[iRec] != NULL_OBJECT
if cbCond = NIL .or. eval(cbCond, oRec)
nSum += oRec:score
endif
endif
next
endif
RETURN nSum
End CLASS
In the above code we can see Method1 has first for loop to iterate aArray of length 1500 objects. Method1 is calling CalculateSum method.
In CalculateSum method, we are using same same aArray to find sum of score but we are taking only those objects that are satisfying cbCondition.
Instead of having for loop in CalculateSum method to check if object in aArray satisfies cbCondtion, is there any optimal way to filter aArray to have only those elements that satisfy cbCondition
And then get the sum of scores for those elements…
I’m new to X# and facing few performance issues the code written in xSharp.
We have an array of 1500 objects. We are looping through each element of this array for some calculation and assigning it’s value to a list.
In the calculation part we have another method that is using same array to loop through and calculating sum based on a condition. This is resulting in nested loops (1500 x 1500) and causing too much delay(about 9 mins) in processing this method.
Could you please suggest me the optimal way to filter the array based on required condition instead of scanning through each element of entire array.
CLASS A
aArray as Array //this is array of objects
METHOD Method1
nLen := aLen(aArray)
for n := 1 upTo nLen
currentElelment := aArray[n]
//some calculation
nSum := CalculateSum(currentDate, DueDate)
//assigning cuurentElement to list
Next
METHOD CalculateSum(currentDate, DueDate)
cbCondition := {|o| o:validDate(currentDate) .and. DateTime2cDate(o:inRangeDate(currentDate)) <= DueDate}
if (iRecs:= aArray:count) > 0
for iRec := 0 upTo iRecs //looping through same array of 1500 objects
if (oRec := aArray[iRec] != NULL_OBJECT
if cbCond = NIL .or. eval(cbCond, oRec)
nSum += oRec:score
endif
endif
next
endif
RETURN nSum
End CLASS
In the above code we can see Method1 has first for loop to iterate aArray of length 1500 objects. Method1 is calling CalculateSum method.
In CalculateSum method, we are using same same aArray to find sum of score but we are taking only those objects that are satisfying cbCondition.
Instead of having for loop in CalculateSum method to check if object in aArray satisfies cbCondtion, is there any optimal way to filter aArray to have only those elements that satisfy cbCondition
And then get the sum of scores for those elements…