Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Why is the app so slow

Why is the app so slow

Scheduled Pinned Locked Moved The Weird and The Wonderful
rubyhelplearning
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BadKarma
    wrote on last edited by
    #1

    I am forced to solve a bug in our VB application, the original programmer has left the building a long time ago. Due to the long and complex function they have given the variable a simple name.

    Dim nFoo() As Integer
    Dim sfoo() As String
    Dim sFoo1() As String
    

    So i went on looking what could be placed inside a nFoo, and i stumbled on this gem.

    j = 0
    For i = 1 To lvwCardGrp.ListItems.Count
      ReDim Preserve nFoo(j)
      nFoo(j) = Mid$(lvwCardGrp.ListItems(i).Key, 3)
      j = j + 1
    Next
    

    And of course why change a winning strategy the same for the sfoo's.

    Learn from the mistakes of others, you may not live long enough to make them all yourself.

    P G P 3 Replies Last reply
    0
    • B BadKarma

      I am forced to solve a bug in our VB application, the original programmer has left the building a long time ago. Due to the long and complex function they have given the variable a simple name.

      Dim nFoo() As Integer
      Dim sfoo() As String
      Dim sFoo1() As String
      

      So i went on looking what could be placed inside a nFoo, and i stumbled on this gem.

      j = 0
      For i = 1 To lvwCardGrp.ListItems.Count
        ReDim Preserve nFoo(j)
        nFoo(j) = Mid$(lvwCardGrp.ListItems(i).Key, 3)
        j = j + 1
      Next
      

      And of course why change a winning strategy the same for the sfoo's.

      Learn from the mistakes of others, you may not live long enough to make them all yourself.

      P Offline
      P Offline
      Paul Conrad
      wrote on last edited by
      #2

      Nice gem :laugh:

      "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

      1 Reply Last reply
      0
      • B BadKarma

        I am forced to solve a bug in our VB application, the original programmer has left the building a long time ago. Due to the long and complex function they have given the variable a simple name.

        Dim nFoo() As Integer
        Dim sfoo() As String
        Dim sFoo1() As String
        

        So i went on looking what could be placed inside a nFoo, and i stumbled on this gem.

        j = 0
        For i = 1 To lvwCardGrp.ListItems.Count
          ReDim Preserve nFoo(j)
          nFoo(j) = Mid$(lvwCardGrp.ListItems(i).Key, 3)
          j = j + 1
        Next
        

        And of course why change a winning strategy the same for the sfoo's.

        Learn from the mistakes of others, you may not live long enough to make them all yourself.

        G Offline
        G Offline
        Graham Bradshaw
        wrote on last edited by
        #3

        In the original coders defense, if there (previously) was a condition around the ReDim / nFoo = / j = lines, the logic makes perfect sense.

        B P 2 Replies Last reply
        0
        • G Graham Bradshaw

          In the original coders defense, if there (previously) was a condition around the ReDim / nFoo = / j = lines, the logic makes perfect sense.

          B Offline
          B Offline
          BadKarma
          wrote on last edited by
          #4

          Graham Bradshaw wrote:

          In the original coders defense, if there (previously) was a condition around the ReDim / nFoo = / j = lines, the logic makes perfect sense.

          Actually in one of the three cases this was true, but then one could do it like this (which is what i chose)

          j = 0
          ReDim nFoo(lvwCardGrp.ListItems.Count - 1)
          For i = 1 To lvwCardGrp.ListItems.Count
            if some_condition then
              nFoo(j) = Mid$(lvwCardGrp.ListItems(i).Key, 3)
              j = j + 1
            endif
          Next
          ReDim nFoo(j - 1)
          

          In this case only one allocation happens. And in the end free up the unnecessary space, without the preserve statement which needs to copy the data to a new location.

          Learn from the mistakes of others, you may not live long enough to make them all yourself.

          G 1 Reply Last reply
          0
          • G Graham Bradshaw

            In the original coders defense, if there (previously) was a condition around the ReDim / nFoo = / j = lines, the logic makes perfect sense.

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            I don't see how. Unless I read it wrong, j is unnecessary, the array should be redimed before the loop, we already know how many items it needs to hold.

            G 1 Reply Last reply
            0
            • B BadKarma

              I am forced to solve a bug in our VB application, the original programmer has left the building a long time ago. Due to the long and complex function they have given the variable a simple name.

              Dim nFoo() As Integer
              Dim sfoo() As String
              Dim sFoo1() As String
              

              So i went on looking what could be placed inside a nFoo, and i stumbled on this gem.

              j = 0
              For i = 1 To lvwCardGrp.ListItems.Count
                ReDim Preserve nFoo(j)
                nFoo(j) = Mid$(lvwCardGrp.ListItems(i).Key, 3)
                j = j + 1
              Next
              

              And of course why change a winning strategy the same for the sfoo's.

              Learn from the mistakes of others, you may not live long enough to make them all yourself.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              That's just too bad. Way too bad.

              Deja View - the feeling that you've seen this post before.

              My blog | My articles

              1 Reply Last reply
              0
              • P PIEBALDconsult

                I don't see how. Unless I read it wrong, j is unnecessary, the array should be redimed before the loop, we already know how many items it needs to hold.

                G Offline
                G Offline
                Graham Bradshaw
                wrote on last edited by
                #7

                PIEBALDconsult wrote:

                we already know how many items it needs to hold.

                Not if you only add items to the array in some condition, which was the point I was trying to make. See the other reply to my post for an example.

                P 1 Reply Last reply
                0
                • B BadKarma

                  Graham Bradshaw wrote:

                  In the original coders defense, if there (previously) was a condition around the ReDim / nFoo = / j = lines, the logic makes perfect sense.

                  Actually in one of the three cases this was true, but then one could do it like this (which is what i chose)

                  j = 0
                  ReDim nFoo(lvwCardGrp.ListItems.Count - 1)
                  For i = 1 To lvwCardGrp.ListItems.Count
                    if some_condition then
                      nFoo(j) = Mid$(lvwCardGrp.ListItems(i).Key, 3)
                      j = j + 1
                    endif
                  Next
                  ReDim nFoo(j - 1)
                  

                  In this case only one allocation happens. And in the end free up the unnecessary space, without the preserve statement which needs to copy the data to a new location.

                  Learn from the mistakes of others, you may not live long enough to make them all yourself.

                  G Offline
                  G Offline
                  Graham Bradshaw
                  wrote on last edited by
                  #8

                  Shouldn't ReDim nFoo(j - 1) be ReDim Preserve nFoo(j - 1)? As the code is, you reinitialise nFoo.

                  B 1 Reply Last reply
                  0
                  • G Graham Bradshaw

                    PIEBALDconsult wrote:

                    we already know how many items it needs to hold.

                    Not if you only add items to the array in some condition, which was the point I was trying to make. See the other reply to my post for an example.

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    Ah, I see, I did read it wrong.

                    1 Reply Last reply
                    0
                    • G Graham Bradshaw

                      Shouldn't ReDim nFoo(j - 1) be ReDim Preserve nFoo(j - 1)? As the code is, you reinitialise nFoo.

                      B Offline
                      B Offline
                      BadKarma
                      wrote on last edited by
                      #10

                      Graham Bradshaw wrote:

                      Shouldn't ReDim nFoo(j - 1) be ReDim Preserve nFoo(j - 1)? As the code is, you reinitialise nFoo.

                      Yes, you are correct. Thanks for pointing it out, must have slipped through my fingers while typing it over from the real code. :-O Note to myself: Learn to use Copy-Past ;)

                      Learn from the mistakes of others, you may not live long enough to make them all yourself.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups