See the VBEditor's helpfiles.
See my code (especially the amount of...)
Compare the speed of both methods
The amount of code is a fascinating topic in my opinion. However, I don't think the number of lines of code will make run-times quicker or slower. What the code does determines the speed, am I wrong?
The following is all I get from VBEditor helpfile:This doesn't tell me why, only that there is another technique to adding values.In a ListBox or ComboBox with a single column, the AddItem method provides an effective technique for adding an individual entry to the list. In a multicolumn ListBox or ComboBox, however, the List and Column properties offer another technique; you can load the list from a two-dimensional array.
On speed you can't 'think', you have to test (please feel free to do so).
Afteer that we'll continue.
I posted an amended version of the attachment (replaced the 'old' one).
I'm trying to test the speed but either I am doing something wrong or the code runs quicker than the time value that is outputted. I have the following code being used:It should be noted that this is my first attempt to test the speed and I'm obviously doing something wrong.Please Login or Register to view this content.
Try this, it's better for short durations
See here for a description
http://support.microsoft.com/kb/172338
Please Login or Register to view this content.
Last edited by Kyle123; 08-24-2011 at 02:59 PM.
I've just run the code below 5 times:
The results are pretty conclusive:Please Login or Register to view this content.
Please Login or Register to view this content.
Hi Mordred,
If your question is "should .AddItem be used or not?" (the thread title) then the answer is it depends on the situation. AddItem is very good for putting a single value (or a couple of values) into the control, whilst List works well with larger sets of items (arrays).
In case you're wondering, yes, you could use List to populate a single item by using a single-element array, eg
But that would be a lot slower than AddItem.Please Login or Register to view this content.
Also bear in mind that AddItem can be used to append values when there are already some in the control (provided the existing values are not bound), whereas List cannot (directly). They're different tools for different situations.
Using Kyle's code on my computer, it's only when there are > 5 items that List starts to provide any performance benefits over AddItem. I've appended the code/results at the bottom of the post.
Correct. You recently saw a great example of this over at XVBT, where the longer version of the code was not only faster than the one-liner, it was more readable, robust and maintainable.Originally Posted by Mordred
Please Login or Register to view this content.
Please Login or Register to view this content.
Last edited by Colin Legg; 08-24-2011 at 05:27 PM.
My result:
List : 3,840027
Add item 5,710022
using:
Please Login or Register to view this content.
Last edited by snb; 08-24-2011 at 06:01 PM.
My version of Kyle's code uses 1000 test iterations on a single listbox because the timer isn't sensitive to pick up any difference on a single iteration. To make each iteration equal, the state of the listbox needs to be the same (ie. cleared). This meant that I had to include .Clear to reset the Listbox for the AddItem method. It's a means to and end to make the tests equivalent - not a practical requirement - because the 'test environment' has a single listbox which is being hammered. I want to compare .AddItem and .List directly, so .Clear needs to accompany the .List method too in order to maintain the status quo. It doesn't matter, for timing purposes, that the .List method 'clears' the Listbox in itself - we have to keep the playing field level to make it a fair test. If you include .Clear for both so that you are directly comparing the speed of the .AddItem and .List calls alone, you might see that .AddItem is quicker for 5 items. I did, anyway. An alternative test would be to set up 2000 listboxes, iterate through 1000 of them using .List, iterate through the other 1000 using .AddItem, and compare how long they took - that way you avoid using .Clear altogether.
But I don't want to get side-tracked on how many items it takes to make one faster than the other: the point of my last post was that they are different tools and both have their rightful place in the developer's toolkit.
Last edited by Colin Legg; 08-25-2011 at 04:17 AM.
Taking this a little further, I have a requirement to bulk add items to a list box already populated and didn't want to be hit with the .AddItem slow down, so I bodged together the following - despite the looping it is still much faster than looping .AddItem calls (for substantial additions):Also bear in mind that AddItem can be used to append values when there are already some in the control (provided the existing values are not bound), whereas List cannot (directly). They're different tools for different situations.
In the Class:
An example implementation:Please Login or Register to view this content.
Please Login or Register to view this content.
Last edited by Kyle123; 08-25-2011 at 07:29 AM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks