Welcome, Guest
Username: Password: Remember me
Welcome to the XSharp forum!

Tell us and our members who you are, what you like and why you became a member of this site.
We welcome all new members and hope to see you around a lot!
  • Page:
  • 1

TOPIC:

Need help on sorting array of objects 28 Apr 2022 16:50 #22316

  • LathaP
  • LathaP's Avatar
  • Topic Author


  • Posts: 1
  • 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…

    Please Log in or Create an account to join the conversation.

    Need help on sorting array of objects 28 Apr 2022 19:34 #22318

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3366
  • 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:
    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

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Need help on sorting array of objects 29 Apr 2022 00:28 #22319

    • Chris
    • Chris's Avatar


  • Posts: 3981
  • 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?

    .
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1