Post

 Resources 

Console

Home | Profile | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 VBGamer
 VBGamer
 2 vb.net speed test results and apps/code

Note: You must be registered in order to post a reply.

Screensize:
UserName:
Password:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkInsert EmailInsert Image Insert CodeInsert QuoteInsert List Spell Checker
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

   Insert an File
Check here to include your profile signature.
Check here to subscribe to this topic.
    

T O P I C    R E V I E W
cbx Posted - Nov 28 2004 : 5:12:05 PM
I was playing around in vb.net 2002 and was curious about the performance and speed comaprisons between using "Redim" or "Redim Preserve" to redim an array. I originally thought Redim Preserve would be faster then Redim but turns out I was wrong! it's about 2 times faster to use Redim over Redim Preserve! Important to know if you are resizing an array and you don't need to hold onto the existing entries in that array. I thought Redim Preserve would be faster simply because resizing the array to a larger size would require less effort seeing as how only the new items in the array would need to be allocated. BTW do not drag the slider over to the right to fast or else you will get a out of mem exception and will have to restart the app. My machine has 2gig of memory and cannot allocate an array with more then 20,000,000 entries or so.

I also wanted to compare clearing or setting up default values of the entries in an array, using the Array.CopyTo method or using a For loop to set the default values. Turn out I was wrong again! I originally suspected Array.CopyTo would be faster but turns out using a simple for loop is actually over 2 times faster! Even using the Array.Copy method is 22% slower or more then using a for loop.

Download Attachment: ArrayCopyToClearVersesForLoop.zip<br>12.26 KB

Download Attachment: RedimVersesRedimPreserve.zip<br>12.07 KB
3   L A T E S T    R E P L I E S    (Newest First)
cbx Posted - Nov 29 2004 : 12:12:43 AM
I get 100 Milliseconds for the Console App and 2 milliseconds for the vb6 app. Thanks for the memory allocation tip! I did not know that! Actually I had never really put any thought into it. I had just resonably assumed that allocating a small ammount of memory would take less time, as opposed to having to allocate the entire array all over again. But I guess now that you put it that way it makes more sence as I can begin to see comparisons between hard disk allocation and memory allocation...
Eric Coleman Posted - Nov 28 2004 : 10:58:47 PM
quote:
Originally posted by cbx
<br> because resizing the array to a larger size would require less effort seeing as how only the new items in the array would need to be allocated.



Memory allocation doesn't work like that When redimming (or reallocating memory) an array, an entirely new section of memory is allocated. The Peserve keywork simply means that the old section of memory is copied into the new section of memory. Blocks of memory can not be appended or extended, this is a limitation of how memory is organized on a very low level.

In C, you use Malloc and Free to allocate and dealloate memory. When calling Malloc to get chunk of memory it traverses an internal linked list of blocks of memory untill it finds something large enough to hold what you need. It then breaks that chunk into two pieces, one for you, and the other to go back into the linked list of free memory chunks. When calling Free, your allocated memory is simply inserted into the linked list. Eventually memory gets fragmented and on occasion a call to Malloc will cause it to traverse its memory list and see where it can join together adjacent blocks of memory.

Higher level languages, like VB, add another layer of abstraction on top of this. Classic VB used reference counting and VB.NET uses garbage collection. In Classic VB (3,4,5,6), when you Redim an array, the old memory is released. In VB.NET, the old memory sticks around and waits to be garbage collected. From my experience, the garbage collection doesn't happen when the program is doing anything, i.e. it seems to wait for when the application is idle which doesn't really happen with video games since you have a game loop always rendering frames.

--link removed-- demonstrates a very large difference (at least it does for me on my 600 Mhz Pentium 3, with faster processors the tests will be faster) between VB6 and VB.NET with regards to memory management. VB6 is over 100 times faster than VB.NET.
cbx Posted - Nov 28 2004 : 5:18:45 PM
Forgot to mention my timings for the speed tests. I get the fallowing results from each test after being run multiple times...

---------------------------
ArrayCopyToClearVersesForLoop
---------------------------
CopyTo Total Time: 554157 - Seconds: 0.154812133944398 - For Loop Total Time: 420780 - Seconds: 0.117551253022381

I get the same times using the Array.Copy method.
---------------------------
RedimVersesRedimPreserve
---------------------------
Redim Total Time: 474586 - Seconds: 0.132582772391463 - Redim Preserve Total Time: 1194693 - Seconds: 0.333755547143562
19920319 <- number of array entries

VBGamer © Go To Top Of Page
This page was generated in 0.09 seconds. Snitz Forums 2000

Copyright © 2002 - 2004 Eric Coleman, Peter Kuchnio , et. al.