Download Attachment: [url="http://www.vbgamer.com/msgboard/uploaded/cbx/20041128165710_ArrayCopyToClearVersesForLoop.zip"][img]icon_paperclip.gif[/img]ArrayCopyToClearVersesForLoop.zip[/url]<br>12.26 KBDownload Attachment: [url="http://www.vbgamer.com/msgboard/uploaded/cbx/20041128165725_RedimVersesRedimPreserve.zip"][img]icon_paperclip.gif[/img]RedimVersesRedimPreserve.zip[/url]<br>12.07 KB">
2 vb.net speed test results and apps/code
cbxI 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: [url="http://www.vbgamer.com/msgboard/uploaded/cbx/20041128165710_ArrayCopyToClearVersesForLoop.zip"][img]icon_paperclip.gif[/img]ArrayCopyToClearVersesForLoop.zip[/url]<br>12.26 KB Download Attachment: [url="http://www.vbgamer.com/msgboard/uploaded/cbx/20041128165725_RedimVersesRedimPreserve.zip"][img]icon_paperclip.gif[/img]RedimVersesRedimPreserve.zip[/url]<br>12.07 KB
cbxForgot 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
Eric Coleman
quote:
Originally posted by cbx
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. http://www.webglider.com/eric/junk/vb6vsvbnet.zip 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.
cbxI 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...