Solved

100 Point:The performance of Visual Basic function ReDim function is drastically:

Posted on 2001-06-20
17
333 Views
Last Modified: 2011-09-20
Hi Experts:
The performance of Visual Basic function ReDim function is drastically decreased on Windows 98 than Windows 2000.

1) Problem Summary
The VB ReDim function used for memory allocation takes four times long
(average) to run on Windows 95/98 compared with Windows 2000/NT.

(2) Conditions
The problem always occurs on the same PC even if the OS is altered.

(3)Use the attached source to repeat the process which uses the ReDim function
to allocate the following array in a loop of 10,000 times. The following
table shows the results according to the OSs. It shows the time difference
is four times between different OSs.

Number of Array?@Windows 98?@Windows 2000?@Ratio of performance time
(98/2000)
(a) 2,000 array?@0.270 sec.?@0.051 sec.    Aprox 5.1 times
(b) 5,000 array?@0.571 sec.?@0.154 sec.?@?@Aprox 4.1 times
(c)10,000 array?@1.072 sec.?@0.293 sec.?@?@Aprox 3.6 times

(4) Questions
(a) Is this behavior by design or a bug?
(b) If it is by design, are there any workaround?

'2000Redim
Private Sub Command10_Click()
Dim LoopCnt As Long

    For LoopCnt = 1 To 10000
        ReDim DataArray(2000) As Long
        Erase DataArray
    Next LoopCnt

End Sub

'5000 Redim
Private Sub Command7_Click()
Dim LoopCnt As Long

    For LoopCnt = 1 To 10000
        ReDim DataArray(5000) As Long
        Erase DataArray
    Next LoopCnt

End Sub

'1000 Redim
Private Sub Command14_Click()
Dim LoopCnt As Long

    For LoopCnt = 1 To 10000
        ReDim DataArray(10000) As Long
        Erase DataArray
    Next LoopCnt

End Sub
0
Comment
Question by:vbhelper
  • 2
  • 2
  • 2
  • +9
17 Comments
 
LVL 3

Expert Comment

by:Maxim10553
ID: 6212589
homework?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 6212722
Why would someone want to redim 10000 times a array?

Try to declare LoopCnt as Integer. I think that it can help on Win98.
0
 
LVL 100

Accepted Solution

by:
mlmcc earned 0 total points
ID: 6212802
Another factor in the equation is the erase array.  I'll bet the redim of the array is relatively fast and the same in Win 95 and 2000 but the erase array takes much longer in Win 95.

Are you using the same executable on both machines?  If you are running from the IDE then that may also contribute to the difference.

What code is being used to get the time checks? It could also contribute.  

Are there any programs known or unknown running with this application?

I can think of many reasons why one is faster than the other.  Remember Win98 was built and optimized for slower machines therefore it may have code in it to wait for memory delays.  Win 2000 is new and is optimized for the newer faster machines.  It may also be abe to take advantage of the faster bus or faster memory.

If you think this is really a problem, then I suggest you ask Microsoft if you can do anything to speed up your Win 98.

Looking at your timing results, even if the redim took all the time you are looking at 0.000135 sec (worst case (the 2000 iteration)) to redim a relatively large array.  I don't think your users will notice the difference.  If they do tell them to upgrade to Win 2000 or get a faster machine and optimize the Win 98.

You probably need to worry about the big problems in your code before you try to make a millisecond operation faster.

good luck
mlmcc
0
 
LVL 3

Expert Comment

by:Hornet241
ID: 6213029
If you want to try to test the ReDim function try this

Dim DataArray() as Long

Sub Command1_Click()

   For LoopCnt = 0 To 99999
       ReDim Preserve DataArray(LoopCnt) As Long
   Next LoopCnt

End Sub

I have done this and got .361 sec W2K, AMD K6 266, 80 Ram

mimcc is right it is the 'Erase' that is taking all the time
0
 
LVL 2

Expert Comment

by:kmv
ID: 6213193
Could you use next code
 
   For LoopCnt = 1 To 10000
       ReDim DataArray(2000) As Long
       DataArray(10)=10
       Erase DataArray
   Next LoopCnt

Problem may be in next. When you allocate memory (ReDim) memory isn't really allocated. But when you use it it is really allocated.

Where is your 100 points? I see only 0 now. You didn't graded answers on your 7 previous questions. Isn't they really answered? Please review them.

Best regards.
0
 
LVL 1

Expert Comment

by:Aerocom
ID: 6213216
As your array size is max 10,000 change the dim to Integer  which uses 2 bytes rather than the 4 bytes used by Long.

Win 98  is also less efficient in  memory usage  so is most probably using hard drive rather than RAM

 
0
 

Expert Comment

by:gargjapan
ID: 6213325
KMV!
I tried your code that doesn't make it any difference.
0
 

Author Comment

by:vbhelper
ID: 6213332
Gargjapan is right.!
That doesn't make any difference.
Regarding the point. I'll update but I should also get resonable answer for  that.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:vbhelper
ID: 6213392
Gargjapan is right.!
That doesn't make any difference.
Regarding the point. I'll update but I should also get resonable answer for  that.
0
 
LVL 18

Expert Comment

by:deighton
ID: 6213536
Programs that allocate and deallocate large amounts of memory run much worse on win 9x & ME than on NT/2000.
0
 

Expert Comment

by:gargjapan
ID: 6214251
Please tell something that which we don't know.
0
 
LVL 2

Expert Comment

by:kmv
ID: 6214803
Guys! You don't need to tell it loud. Why you do it so emotional?
I just suppose one source of problem. If you see same result - it is fine. Source may be other.
mlmcc told you other source. Hornet241 show you sample code. Do you want to discuss their idea? How deep do you want to investigate this problem? Your description is undetailed. It really seems as homework.
Unfortunatelly we don't know level of your knowledge.
So we don't know what tell you. It is better not to discuss this problem than listen that you already know it. Please write here list of points which you are know, may be we give you information which you don't know.
I can add only one: RTFM, RTFM and RTFM.
May be book Jeffrey Richter "Advanced Windows" will help you.
Best regards.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6216515
A few issues:

Yes, Win2000 is more efficient with memory usage than Win98, but I seem to remember hearing about problems where Win95/98 would not even recognize more than 64MB RAM, so that could be an issue.

Your registry could be part of the problem, depending on how clogged up it is.  It's probably new on your Win2000 installation, but may not be on the Win98 install.

Why would you want to erase an array after you redim it (since redim without preserve will erase it anyway.)

If you need to redim that many times in a loop, it's time to consider some alternatives such as redim it every 10th time:

K=10
dim x() as long
for Z=1 to 10000
  if ((z-1) mod K) = 0 then
     redim x(Z-1+K)
  endif
next z

Sure, you may end up with a few blank spaces (up to K-1), but you'll dramatically improve the loop speed...and your can always remove them after the loop.

     
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6418375
It's time to clean up this topic area and that means taking care of this question. Your options at this point are:
 
1. Award points to the Expert who provided an answer, or who helped you most. Do this by clicking on the "Accept Comment as Answer" button that lies above and to the right of the appropriate expert's name.
 
2. PAQ the question because the information might be useful to others, but was not useful to you. To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.
 
3.  Ask Community Support to help split points between participating experts.  Just comment here with details.
 
4.  Delete the question because it is of no value to you or to anyone else.  To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.
 
If you elect for option 2, 3 or 4, just post comment with details here and I'll take it from there.  We also request that you review any other open questions you might have and update/close them.  Display all your question history from your Member Profile to view details.
 
PLEASE DO NOT AWARD THE POINTS TO ME.
 
____________________________________________
 
 
 
Hi Experts:
 
In the event that the Asker does not respond, I would very much appreciate your opinions as to which Expert ought to receive points (if any) as a result of this question.  Likewise, you can also suggest that I PAQ or delete the question.
 
Experts, please do not add further "answer" information to this question.  I will be back in about one week to finalize this question.
 
Thank you everyone.
 
Moondancer :)
Community Support Moderator @ Experts Exchange
0
 
LVL 3

Expert Comment

by:modder
ID: 6418492
vbhelper and gargjapan are duplicate accounts. What is more, they both have a tendency to abandon their questions as soon as they have their answer.

Their accounts have been closed. Which leaves these open questions as so many loose ends.

Of the participants I would like to ask, please advise. Did anyone provide enough info to deserve the points? Or should this question be deleted?

Thank you

modder
Community Support
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6429171
I'd go with mlmcc
0
 
LVL 3

Expert Comment

by:modder
ID: 6429630
OK. Done.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now