Need help on sorting array of objects

We encourage new members to introduce themselves here. Get to know one another and share your interests.
Post Reply
LathaP

Need help on sorting array of objects

Post by LathaP »

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…
User avatar
wriedmann
Posts: 3783
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Need help on sorting array of objects

Post by wriedmann »

Hi Latha,
if you are rewriting your code I would recommend to not use the VO array, but use .NET style collections, for example the List collection from the System.Collections.Generic namespace
Then you can loop through the list with a simple statement:

Code: Select all

foreach oItem as YourObject in oListOfObjects
  oItem:Calculate()
next
In this manner your list and the objects is strongly typed and should be much, much faster.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Chris
Posts: 4978
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Need help on sorting array of objects

Post by Chris »

Hi Latha,

It is not very clear from your pseudo code, but it looks like CalculateSum() always returns the same value for the same element? If yes, can't you store the results of each item in another array before entering your main loop and simply reuse the value already calculated in a single loop?

Unless it is more complicated than that, but in order to make better suggestions, we need more information. How about a compilable sample code showing exactly what you are doing?

.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
Post Reply