Results 1 to 10 of 10

Thread: VB Speed Tip

  1. #1

    Thread Starter
    Hyperactive Member singularis's Avatar
    Join Date
    Nov 2006
    Location
    Over There!
    Posts
    372

    Red face VB Speed Tip

    Hello,

    I am writing a game using DirectX and have a question about how to optimize speed.

    Would the program run faster if:

    Each object had its own raw vertex data and when drawing them their data is used. This would use lots or varibles but it loop would be tighter

    E.g.

    VB Code:
    1. Private type gameObjects
    2. V(3) as D3DTLVERTEX
    3. X as single
    4. Y as single
    5. End Type
    6.  
    7. Dim Things(100) as gameObjects
    8.  
    9. Private Sub Form_load()
    10. 'Intialize all the game objects vertex data and store it in the objects
    11. End Sub
    12.  
    13. Function DrawMe(what as integer)
    14. Device.DrawPrimativeUP Things(i).V(0),2
    15. End Function

    OR

    Each object did not store its vertex data and a local varible in the drawing function stored the result of generated vertex data. This would use less varibles but the loop would slower.

    E.g.

    VB Code:
    1. Private type gameObjects
    2. X as single
    3. Y as single
    4. End Type
    5.  
    6. Dim Things(100) as gameObjects
    7.  
    8. Function DrawMe(what as integer)
    9. Dim v(3) as D3DTLVERTEX
    10. 'Generate the vertex data and store it in V
    11. Device.DrawPrimativeUP v(0),2
    12. End Function
    If what I said was helpful, give me rep!

    My Complete Games: -- 2D Zone (Space Shooter game) || _2D Zone 2_ || Ninja Blob (2D platformer) || Dren (Co-op up to 4 player base defence game)

    My Projects: -- The Dread Engine (2D VB game Engine) || A* Path Finding


    An excellent site for learning DirectX7, 8 & 9 (for VB6, C# & VB.net) would be: directx4vb.vbgamer.com --- For my projects and games see: pieper.freehostia.com

  2. #2
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: VB Speed Tip

    I don't know anything about DirectX.
    Just in general, Long is a lot faster than Integer. So, if possible, change "what" to long.

    You can do the speed test yourself by using the GetTickCount API. See the CopyMemory example near the bottom of that page.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: VB Speed Tip

    1) Why is DrawMe a function? It doesn't return anything. Setting a function up takes more time than setting a sub up.

    2) Why are you passing DrawMe a parameter if you aren't using it? It takes time to pass parameters.

    3) Inline code is always faster, but it's also larger. You have to trade speed and size.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  4. #4
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: VB Speed Tip

    Speaking of inlining, this alone:

    VB Code:
    1. Device.DrawPrimativeUP Things(i).V(0),2

    is faster than this:

    VB Code:
    1. Function DrawMe(what as integer)
    2. Device.DrawPrimativeUP Things(i).V(0),2
    3. End Function

    And like iStank said (just kidding), Longs are always faster than Integers. Long is a 32 bit data type that works well with 32 bit processors. Integers (16 bit) have to be made into two to get to be 32 bit, go through a process to get the highs and lows, and process, hence why it's slower.

  5. #5

  6. #6

    Thread Starter
    Hyperactive Member singularis's Avatar
    Join Date
    Nov 2006
    Location
    Over There!
    Posts
    372

    Talking Re: VB Speed Tip

    Thanks for the replies, I usually use longs anyway.

    Does inlining code really make a difference? I mean does size have a negative impact on speed?

    And my major question would be "Does creating small local varibles in loops slow down loops and decrease performance or does creating excess varibles need the start of the program use up memory and slow down the program?"


    PS: iPrank is a C# interface
    If what I said was helpful, give me rep!

    My Complete Games: -- 2D Zone (Space Shooter game) || _2D Zone 2_ || Ninja Blob (2D platformer) || Dren (Co-op up to 4 player base defence game)

    My Projects: -- The Dread Engine (2D VB game Engine) || A* Path Finding


    An excellent site for learning DirectX7, 8 & 9 (for VB6, C# & VB.net) would be: directx4vb.vbgamer.com --- For my projects and games see: pieper.freehostia.com

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VB Speed Tip

    In cases like you showed, inlining will make a huge difference to speed.. but then unless you can put the code into a loop, it will become very large if you add to it (such as "'Generate the vertex data").

    The size of a sub/function does not affect speed.. only the readability/maintainability (it becomes easier to make mistakes/introduce bugs). If you can use a loop tho, this is not really an issue.


    Having lots of variables can use up too much memory, which will slow things down.. but for that to happen you would have to have lots of large variables (an array of 250000 Singles uses less than 1MB!), or object variables which use a large amount of memory (how much they use depends on the objects).

    As to creating variables in loops, it doesn't matter (in simple terms, the variable is actually created when the sub/function starts). What matters is the extra work you are doing with them... if you are aiming for speed, you should have everything you can outside of loops.


    There is a good article about optimising code in our Classic VB FAQ's (link in my signature), which should help you to understand some of the things you could do to make your program faster.

  8. #8

    Thread Starter
    Hyperactive Member singularis's Avatar
    Join Date
    Nov 2006
    Location
    Over There!
    Posts
    372

    Re: VB Speed Tip

    Thanks, I will take a look at your link
    If what I said was helpful, give me rep!

    My Complete Games: -- 2D Zone (Space Shooter game) || _2D Zone 2_ || Ninja Blob (2D platformer) || Dren (Co-op up to 4 player base defence game)

    My Projects: -- The Dread Engine (2D VB game Engine) || A* Path Finding


    An excellent site for learning DirectX7, 8 & 9 (for VB6, C# & VB.net) would be: directx4vb.vbgamer.com --- For my projects and games see: pieper.freehostia.com

  9. #9
    New Member
    Join Date
    Dec 2003
    Location
    London
    Posts
    15

    Re: VB Speed Tip

    Just for your information, GetTickCount is useless for speed profiling code, it only has a resolution of milliseconds. Use these functions:

    Public Declare Function QueryPerformanceCounter Lib "kernel32" Alias "QueryPerformanceCounter" (lpPerformanceCount As LARGE_INTEGER) As Long

    Public Declare Function QueryPerformanceFrequency Lib "kernel32" Alias "QueryPerformanceFrequency" (lpFrequency As LARGE_INTEGER) As Long

    They reference hardware clocks will give resolutions of a 1000 times better, i.e. in microseconds. Also setting the priority of the process to 'Realtime' will inhibit windows from interfering with the timing, but do this with great caution, you won't be able to break out of an endless loop other than switching off the machine..! ;o)

    Salvatore

  10. #10

    Thread Starter
    Hyperactive Member singularis's Avatar
    Join Date
    Nov 2006
    Location
    Over There!
    Posts
    372

    Re: VB Speed Tip

    Thanks, I will look into that, normally I use GetTickCount
    If what I said was helpful, give me rep!

    My Complete Games: -- 2D Zone (Space Shooter game) || _2D Zone 2_ || Ninja Blob (2D platformer) || Dren (Co-op up to 4 player base defence game)

    My Projects: -- The Dread Engine (2D VB game Engine) || A* Path Finding


    An excellent site for learning DirectX7, 8 & 9 (for VB6, C# & VB.net) would be: directx4vb.vbgamer.com --- For my projects and games see: pieper.freehostia.com

Posting Permissions

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



Click Here to Expand Forum to Full Width