Announcement

Collapse
No announcement yet.

Silly question...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Gregery D Engle
    replied
    Mark,

    The reason your code is slow is 1)

    within your for .. loop you are reallocating the array and saving
    the information. What I normally do is this case, is resize the
    array before the for..loop or after or both. never within. Very
    slow performance.

    Code:
    #COMPILE EXE
    #INCLUDE "win32api.inc"
    
    %Max = 100000
    
    TYPE mytype
        n AS STRING * 4096
    END TYPE
    
    FUNCTION PBMAIN AS LONG
        DIM result AS LONG
        DIM hdlg AS LONG
        DIM lut() AS mytype
        DIM i AS LONG
    
    
        DIALOG NEW 0,"test",1,1,256,64,%WS_POPUP OR %WS_CAPTION OR %WS_SYSMENU TO hdlg
        CONTROL ADD LABEL,hdlg,100,"",4,4,200,12
        CONTROL ADD LABEL,hdlg,200,"",4,18,200,12
        DIALOG SHOW MODELESS hdlg
    
        DIALOG DOEVENTS TO result
        REDIM lut(0 TO %Max)
    
          FOR i = 0 TO 100000
            IF INT(i MOD 10) = 0 THEN
                DIALOG DOEVENTS TO result
                IF (result = 0) THEN EXIT FOR
    
                CONTROL SET TEXT hdlg,100,FORMAT$(i,"array reallocated(#)")
            END IF
        NEXT i
    
        DO UNTIL result = 0
            DIALOG DOEVENTS TO result
        LOOP
    END FUNCTION
    - The reason a long runs faster then a string is 1) a long is only
    32 bits, and your string is 32768 bits long. Also there
    has been discussions about that longs are native to the operating
    system within a 32 bit programming environment. Lance would be
    the guy to ask about that.

    - For this test I didn't do it but for a real test I would popuplate
    the UDT via pointer to increase speed. Although, you will
    still have to redim or dim the UDT without pointer.

    Hope that helps,

    ------------------
    -Greg

    Leave a comment:


  • Guest's Avatar
    Guest started a topic Silly question...

    Silly question...

    Hello,


    Why does the following code seem to slow down exponentialy? If I
    substitute the variable "mytype" with a long, the program seems to
    run at a steady speed...

    Does this have anything to do with the "preserve" feature of redim?

    Code:
    #compile exe
    #include "win32api.inc"
    
    
    type mytype
        n as string * 4096
    end type
    
    
    function pbmain as long
        dim result as long
        dim hdlg as long
        dim lut() as mytype
        dim i as long
        
        
        dialog new 0,"test",1,1,256,64,%WS_POPUP or %WS_CAPTION or %WS_SYSMENU to hdlg
        control add label,hdlg,100,"",4,4,200,12
        control add label,hdlg,200,"",4,18,200,12
        dialog show modeless hdlg
        
        
        dialog doevents to result
        
        
        
        for i = 0 to 100000
            redim preserve lut(i)
            
            
            if int(i mod 10) = 0 then
                dialog doevents to result
                if (result = 0) then exit for
            
                control set text hdlg,100,format$(i,"array reallocated(#)")
            end if
        next i
        
        
        
        do until result = 0
            dialog doevents to result
        loop
    end function

    ------------------
    Cheers
Working...
X