Looping function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Charlie Brown

    Looping function

    I am using something like the following function to loop through a
    recipe program I am working on. As I was writing it, I starting
    thinking this isn't going to work at all... but not I'm not so sure.
    Will this code work, or cause me problems?

    Private Sub DoSomething()
    Dim viCount As Integer = _varianceList.C ount
    For i As Integer = 0 To viCount - 1
    Dim vi As ProductionVaria nceItem = _varianceList(i )
    vi.ProductionOu t = GetProductionOu t(vi, _varianceList)
    Next
    End Sub

    Private Function GetProductionOu t(ByVal item As
    ProductionVaria nceItem, ByVal itemList As List(Of
    ProductionVaria nceItem)) As Double
    Dim itemCount As Integer = itemList.Count
    For i As Integer = 0 To itemCount - 1
    Dim vi As ProductionVaria nceItem = itemList(i)

    'do something

    Next
    End Function
  • =?ISO-8859-1?Q?G=F6ran_Andersson?=

    #2
    Re: Looping function

    Charlie Brown wrote:
    I am using something like the following function to loop through a
    recipe program I am working on. As I was writing it, I starting
    thinking this isn't going to work at all... but not I'm not so sure.
    Will this code work, or cause me problems?
    >
    Private Sub DoSomething()
    Dim viCount As Integer = _varianceList.C ount
    For i As Integer = 0 To viCount - 1
    Dim vi As ProductionVaria nceItem = _varianceList(i )
    vi.ProductionOu t = GetProductionOu t(vi, _varianceList)
    Next
    End Sub
    >
    Private Function GetProductionOu t(ByVal item As
    ProductionVaria nceItem, ByVal itemList As List(Of
    ProductionVaria nceItem)) As Double
    Dim itemCount As Integer = itemList.Count
    For i As Integer = 0 To itemCount - 1
    Dim vi As ProductionVaria nceItem = itemList(i)
    >
    'do something
    >
    Next
    End Function
    What is it that you are trying to do?

    That code would loop all combinations of two items, including of course
    combining each item with itself. So, if you have 100 items the "do
    something" code will run 10000 times.

    --
    Göran Andersson
    _____
    Göran Anderssons privata hemsida.

    Comment

    • Tom Shelton

      #3
      Re: Looping function

      On 2008-06-13, Charlie Brown <cbrown@duclaw. comwrote:
      I am using something like the following function to loop through a
      recipe program I am working on. As I was writing it, I starting
      thinking this isn't going to work at all... but not I'm not so sure.
      Will this code work, or cause me problems?
      >
      Private Sub DoSomething()
      Dim viCount As Integer = _varianceList.C ount
      For i As Integer = 0 To viCount - 1
      Dim vi As ProductionVaria nceItem = _varianceList(i )
      vi.ProductionOu t = GetProductionOu t(vi, _varianceList)
      Next
      End Sub
      >
      Private Function GetProductionOu t(ByVal item As
      ProductionVaria nceItem, ByVal itemList As List(Of
      ProductionVaria nceItem)) As Double
      Dim itemCount As Integer = itemList.Count
      For i As Integer = 0 To itemCount - 1
      Dim vi As ProductionVaria nceItem = itemList(i)
      >
      'do something
      >
      Next
      End Function
      Well... _varianceList appears to be a member variable, so if GetProductionOu t
      only works on that list, then it seems pointless to pass it in the arguments
      list - since it can directly access it anyway. So, if that's the case (which
      is unclear from the context) then I would probably change the signature of
      GetProductionOu t to be something like:

      Private Function GetProductionOu t (ByVal item As ProductionVaria nceItem) As Double
      ' do stuff
      End Function

      As for the looping, it's hard to say. There is nothing technically wrong with
      your loops - but, there maybe better ways of doing this. But, it ultimately
      depends on what the code is trying to accomplish. The first loop looks to me
      that it could be written more clearly as a For Each:

      Public Sub DoSomething ()
      For Each vi As ProductionVaria nceItem In _varianceList
      vi.ProductionOu t = GetProductionOu t(vi)
      Next
      End Sub

      Even if I was going to do a for loop (which is slightly faster then a for
      each), then I would most likely write it like this:

      Public Sub DoSomething ()
      For i As Integer = 0 To _varianceList.C ount - 1
      vi.ProductionOu t = GetProductionOu t(_varianceList (i))
      Next
      End Sub

      The same applies to the GetProductionOu t function. Unless you need to use the
      final value of i after the loop - then you might as well declare it in the
      loop, so that you are reducing the scope of i to it's narowest use.

      Anyway, like I said - there doesn't appear to be anything technically wrong
      with your loops or any reason to believe that they won't "work"...

      --
      Tom Shelton

      Comment

      • Tom Shelton

        #4
        Re: Looping function

        On 2008-06-13, Göran Andersson <guffa@guffa.co mwrote:
        Charlie Brown wrote:
        >I am using something like the following function to loop through a
        >recipe program I am working on. As I was writing it, I starting
        >thinking this isn't going to work at all... but not I'm not so sure.
        >Will this code work, or cause me problems?
        >>
        >Private Sub DoSomething()
        > Dim viCount As Integer = _varianceList.C ount
        > For i As Integer = 0 To viCount - 1
        > Dim vi As ProductionVaria nceItem = _varianceList(i )
        > vi.ProductionOu t = GetProductionOu t(vi, _varianceList)
        > Next
        >End Sub
        >>
        >Private Function GetProductionOu t(ByVal item As
        >ProductionVari anceItem, ByVal itemList As List(Of
        >ProductionVari anceItem)) As Double
        > Dim itemCount As Integer = itemList.Count
        > For i As Integer = 0 To itemCount - 1
        > Dim vi As ProductionVaria nceItem = itemList(i)
        >>
        > 'do something
        >>
        > Next
        > End Function
        >
        What is it that you are trying to do?
        >
        That code would loop all combinations of two items, including of course
        combining each item with itself. So, if you have 100 items the "do
        something" code will run 10000 times.
        >
        Yeah, I didn't even consider the performance implications!

        --
        Tom Shelton

        Comment

        • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

          #5
          RE: Looping function

          It appears to me that the value of productionOut for each item in the array
          depends on the properties of all the objects in the array. Therefore, it
          seems to me at least potentially incorrect if later an entry was changed that
          would somehow effect the value of an already computed productionOut.

          In essence, what I mean is, suppose you compute a value of _varianceList
          (5).productionO ut = 7.3. If the computation of _varianceList
          (3).productionO ut needed the value of _varianceList(5 ).productionOut , then it
          would have (possibly) had a different value. Therefore the computation of
          _varianceList(3 ) is in doubt, etc, etc...

          I don't know your subject or problem definition to know if this is a problem
          or not however...

          "Charlie Brown" wrote:
          I am using something like the following function to loop through a
          recipe program I am working on. As I was writing it, I starting
          thinking this isn't going to work at all... but not I'm not so sure.
          Will this code work, or cause me problems?
          >
          Private Sub DoSomething()
          Dim viCount As Integer = _varianceList.C ount
          For i As Integer = 0 To viCount - 1
          Dim vi As ProductionVaria nceItem = _varianceList(i )
          vi.ProductionOu t = GetProductionOu t(vi, _varianceList)
          Next
          End Sub
          >
          Private Function GetProductionOu t(ByVal item As
          ProductionVaria nceItem, ByVal itemList As List(Of
          ProductionVaria nceItem)) As Double
          Dim itemCount As Integer = itemList.Count
          For i As Integer = 0 To itemCount - 1
          Dim vi As ProductionVaria nceItem = itemList(i)
          >
          'do something
          >
          Next
          End Function
          >

          Comment

          • Cor Ligthert[MVP]

            #6
            Re: Looping function

            Tom,
            >>
            >That code would loop all combinations of two items, including of course
            >combining each item with itself. So, if you have 100 items the "do
            >something" code will run 10000 times.
            >>
            >
            Yeah, I didn't even consider the performance implications!
            >
            --
            Not about the methods, but about your message.

            In my idea is this very short as it is about bitmap processing
            I can really not see any performance implications.

            Cor

            Comment

            • Cor Ligthert[MVP]

              #7
              Re: Looping function

              Charlie,

              One of those loops is as I see it completely withouth sense.

              As I see it right they both do exactly the same

              Cor

              Comment

              • =?ISO-8859-1?Q?G=F6ran_Andersson?=

                #8
                Re: Looping function

                Cor Ligthert[MVP] wrote:
                Charlie,
                >
                One of those loops is as I see it completely withouth sense.
                >
                As I see it right they both do exactly the same
                >
                Cor
                No, they don't do the same thing at all. One loop is within the other.

                I don't know if that is what's intended (that's why I asked what he's
                trying to do), but that's what the code does.

                --
                Göran Andersson
                _____
                Göran Anderssons privata hemsida.

                Comment

                • Tom Shelton

                  #9
                  Re: Looping function

                  On 2008-06-14, Cor Ligthert[MVP] <notmyfirstname @planet.nlwrote :
                  Tom,
                  >>>
                  >>That code would loop all combinations of two items, including of course
                  >>combining each item with itself. So, if you have 100 items the "do
                  >>something" code will run 10000 times.
                  >>>
                  >>
                  >Yeah, I didn't even consider the performance implications!
                  >>
                  >--
                  >
                  Not about the methods, but about your message.
                  >
                  In my idea is this very short as it is about bitmap processing
                  I can really not see any performance implications.
                  >
                  Cor
                  It's the number of times the loop runs, and the question of wether it's
                  necessary or not. Somethies, Cor, there are better ways to do things that can
                  improve performance.

                  --
                  Tom Shelton

                  Comment

                  • Cor Ligthert[MVP]

                    #10
                    Re: Looping function

                    >
                    No, they don't do the same thing at all. One loop is within the other.
                    >
                    Yea, but in my idea is the one inside the other doing the same loop
                    completely again

                    Cor

                    Comment

                    • Charlie Brown

                      #11
                      Re: Looping function

                      On Jun 14, 1:58 pm, "Cor Ligthert[MVP]" <notmyfirstn... @planet.nl>
                      wrote:
                      No, they don't do the same thing at all. One loop is within the other.
                      >
                      Yea, but in my idea is the one inside the other doing the same loop
                      completely again
                      >
                      Cor
                      @Everyone
                      Thanks for everyone's input.

                      Yes, it does look a little ridiculous looping through itself, but here
                      is the situation. I have a list of recipes that have been
                      "produced" (i.e. made by people each day). Let's say, today the
                      restaurant has made 32 Qt's of Spicy BBQ Sauce. One of the
                      ingredients of Spicy BBQ sauce is just plain BBQ Sauce. BBQ sauce is
                      also in the list because 10 QT's of it were made for other reasons.

                      So I need to loop through the list and find any items that have
                      ingredients that are also on the same list and then add the right
                      amount to them, so I know the total made.

                      In the end, I used something very similar to what i posted, it seems
                      to working fine, list total count is about 500 and won't creep to much
                      higher than that in the foreseeable future.

                      @Tom Shelton
                      I use the For... Next instead of For Each for performance sake,
                      although I agree it's probably not much to worry about. As for the
                      referencing of the count outside of the loop, that's there for legacy
                      sake, and will get tossed during the next big iteration when i have
                      time to replace all of them, for now I like to keep the code
                      consistent.

                      @Family Tree Mike
                      Exactly. You seem to have hit the nail on the head about what the
                      code does. The biggest problem is that some ingredients are in
                      mutliple recipes. And those recipes can also be ingredients in other
                      recipes. There is some pretty deep nesting involved, nearly 4 levels
                      in some places. This increases the complexity of the loops and does
                      indeed change the .ProductionOut values quite often in the middle of
                      the loop.

                      Comment

                      Working...