Results 1 to 7 of 7

Thread: Array vs type optimization

Share/Bookmark
  1. #1

    Array vs type optimization

    My company has PB/CC and i'm considering using it for project, but I haven't worked with PB before.
    My question is about optimization of types vs arrays.
    I created a type that is just an enumerated collection of LONGs but I can also just use an array of longs to achive the same thing.
    I need the routines that work with this data to be as fast as possible.
    Is there a performance difference if I use a UDT of longs rather than an array of longs?

    Thanks

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,718
    Blog Entries
    3
    Rep Power
    684

    Re: Array vs type optimization

    Hi,

    I am not sure what you mean by "enumerated collection of LONGs".
    Could you post UDT definition here?


    Petr

    P.S. Which release of PB/CC it is?
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

  3. #3

    Re: Array vs type optimization

    I am not sure what you mean by "enumerated collection of LONGs".
    A UDT where where each field is a long variable...

    Its ver.4

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,718
    Blog Entries
    3
    Rep Power
    684

    Re: Array vs type optimization

    So you are thinking whether it is better to use:
    [code=thinbasic]
    TYPE tEnum
    l1 AS LONG
    l2 AS LONG
    l3 AS LONG
    l4 AS LONG
    END TYPE
    [/code]

    or

    [code=thinbasic]
    DIM arr(3) AS LONG ' -- 0..3
    [/code]

    If it is this, I would take pointer to array or UDT anyway, and then I would increase pointer by 4 bytes to go through ( LONG = 32bit ).

    I think array would be more robust solution, you can REDIM it when you need.

    You can also overlay array over UDT:
    [code=thinbasic]
    #COMPILE EXE
    #DIM ALL

    TYPE tEnum
    l1 AS LONG
    l2 AS LONG
    l3 AS LONG
    l4 AS LONG
    END TYPE

    FUNCTION PBMAIN () AS LONG

    ' -- 4 element array
    DIM arr(3) AS LONG

    ' -- 4 element udt
    DIM udtVar AS tEnum

    ' -- Virtual array over UDT
    DIM arrayOverlay(3) AS LONG AT VARPTR(udtVar)

    ' -- Accessing l4 as it would be array
    arrayOverlay(3) = 1024

    ' -- For real!
    MSGBOX STR$(udtVar.l4)

    END FUNCTION
    [/code]

    While PB allows DIM arr(1 TO 4), it is recommended to use syntax I showed, as it results in faster indexing of array.
    When you use pointers you are free to use any dim I presume.

    I cannot give more advice, as optimization always depends from the task which needs to be done.


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

  5. #5

    Re: Array vs type optimization

    Ok, That helps.
    Thanks!

  6. #6
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,718
    Blog Entries
    3
    Rep Power
    684

    Re: Array vs type optimization

    Few general purpose hints:

    - use register variables for FOR/NEXT loops
    [code=thinbasic]
    REGISTER i AS LONG

    ...

    FOR i = 1 TO 1000000
    ...
    NEXT
    [/code]

    - use 32bit variables, prefer LONGs over DWORDs

    - if you do not want to go pointer way, in case of complex structures inside structures it is advatageous to overlay simpler data structure ( DIM ... AT ) over the part you are interested in, as it results in less dereferencing, I guess


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

  7. #7
    thinBasic MVPs Michael Hartlef's Avatar
    Join Date
    Sep 2006
    Location
    Leverkusen, Germany
    Age
    51
    Posts
    3,243
    Blog Entries
    2
    Rep Power
    335

    Re: Array vs type optimization

    Hi Petr,

    interesting topic and the thing about REGISTER, well I didn't know that. Off to optimize some code tonight.

    Michael
    Running Windows 7 Home, 64 bit, 8 GB ram, Athlon II X2 255, ATI Radeon HD 4200.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •