|
To be honest you pose some really good questions that really hit the fundamentals of .NET and their language integration. I don't really know enough to fully answer you.
One thing I can tell you though: memory reallocation is a very expensive operation, no matter what the implementation, and thus using constant ReDim's as the data is coming in is a bad idea. Probably 90% of the processing time will then be taken by the ReDim statement(s).
The List(Of T) on the other hand allocates blocks of memory at a time, so only once the block is filled does it reallocate.
I believe, but have no hard proof for this, that the utility classes .NET provides are all quite fast (in .NET relative terms, of course).
Quite possibly, using a similar system of allocated larger blocks at a time and only reallocating when a block is filled yourself with ReDim's could be a bit faster. At the very least, you'll probably avoid some function call overhead.
You've got me curious now I'll look in to it, barring someone else being able to answer these questions before I get time :P
You could also always write your own test where you time both implementations. One test where you ReDim the array and add a random integer constantly, and the other where you use a List(Of Integer) and then checking the runtime of each on, say, 10000 or so iterations.
EDIT 1:
First test is in. Adding 100,000 integers to a list using ReDim's or List with standard capacity:
VB ReDim's: 8919.503 ms
List(Of Integer): 2.0002 ms
So, ReDim-ing as you can see is really a bad idea. I do have to note though that on 10,000 items the time is not noticeable.
EDIT 2:
Included a ReDim mimic version of List(Of T)'s implementation: doubling capacity every time we overflow. The results are in:"
VB ReDim's: 8907.5024 ms
List(Of Integer): 2.0001 ms
VB ReDim with Capacity: 2.0001 ms
The conclusion is: use List(Of Integer). There is no speed difference noticeable (not even in my test) between implementing it yourself. But List(Of T) handles a lot of things for you, has lots of extra functionality, and negates the need for manual array management = double win.
modified on Tuesday, June 21, 2011 2:01 PM
|
|
|
|
|
additional to your EDIT 2:
did you also test starting with an big array (say 1.000.000) and redim to double size if overflow, and one redim at the end (to reduce to used size)? how fast would this be?
I cannot remember: What did I before google?
|
|
|
|
|
I did not test that, as I didn't think from the OP that an array as large as 1,000,000 would be relevant. The ReDim would be slow if needed, but then again it won't be needed much.
Anyway, it'd be the same performance as starting with New List(Of Integer)(1000000) , then using Add to add the items, and finally calling TrimExcess() on the list.
You can test all this yourself by writing a very simple test program. I used Now() to get the time before and after the operation and output the difference in milliseconds. Took me no more than 10 minutes to write the entire test program and do the tests.
|
|
|
|
|
I tried to save this 10 minutes
I cannot remember: What did I before google?
|
|
|
|
|
I wasn't on my own computer, so I poured all of those 10 minutes into EDIT 1 & 2 :P
|
|
|
|
|
ok, i used a few minutes ... and proofed you are right.
AND i want YOUR computer *g* both ways used 3 milliseconds on my machine
I cannot remember: What did I before google?
|
|
|
|
|
Thank you for running the test and your very helpful answer.
|
|
|
|
|
Read SlimList (I'm the author, but I think it's very relevant to your question). It discusses a bit about how List works. The List class basically uses an array that doubles in capacity each time capacity is reached. If you average out the time spent on each add operation, that comes to a performance of O(1) for each insert operation. Performing an array resize each time you add an item would average to O(N) for each insert operation (where N is the total number of objects inserted). Of course, you could reimplement the capacity-doubling functionality to make it O(1), but then you'd just be making List all over again. Read about big-O notation if you don't know what O(1) and O(N) mean.
Go with List. Read the above article if you want a more in-depth understanding of List (as well as an understanding of how it can theoretically be improved upon). Also, one more note: do not use SlimList. It merely demonstrates a theoretical improvement that is in practice so slow that it is not worthwile to with any real production code.
Steven St. John wrote: I understand that using a collection would be a bad idea because of boxing/unboxing issues, but I was given to believe List(Of T) does not suffer this penalty.
The word "collection" applies to both arrays and Lists. In either case, those collections will only suffer a boxing penalty if you try to store a non-reference type in a reference variable. For example, if you put an integer into a List(Of Object) . However, putting an integer into a List(Of Integer) would not cause boxing.
|
|
|
|
|
Thank you - your article was very informative!
|
|
|
|
|
I'm glad it helped.
|
|
|
|
|
Dear All,
Kindly advise me how can I be able to write Urdu in textboxes/listboxes/... during run time in VB 6.0. Even support for Urdu is installed on my system (CRULP URDU phonetic Keybaord) and "Microsoft Form Object 2.0 Library" is also enabled. Some others character similar to German language were usually displayed from Left to Right fashion during typing in textbox.
Regards
|
|
|
|
|
This[^] may help.
------------------------------------
I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave
CCC Link[ ^]
Trolls[ ^]
|
|
|
|
|
Hi,
I want to show alert on my windows form when a new record added to database. I want to show the no. of rows added in a link label and on clicking on the link I want to show the rows newly added to database.
Kindly suggest how this will be possible.
Thanks
Sanjay
|
|
|
|
|
You would need to poll the database, perhaps a timer or thread could be used, and check if any changes had been made. If changes exist show the LinkList. If the user clicks on the links then retrieve the record/s from the db and show them.
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
|
|
|
|
|
You could place a Timer control on your form like Wayne suggested and the code behind would be something like this ...
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick<br />
' place code here to check if a new record has been added to the database<br />
End Sub
Your request is something kind of unclear because I don't see what the real-world applicaiton would be.
If it is a dispatching system, then I would create a "Pending Tasks" list and have a function that would refresh this list on a periodic basis. The problem that I see is ... "What is considered a new record and what are you going to do with it?" You might need to flag it in some way that the record has been processed. What if your window application is not running and someone adds a record to the database ?
You might want to revisit your design.
Just some thoughts
Good luck.
|
|
|
|
|
I wrote a large Database program in VB6 that worked well for many years. Now I am trying to re-write that program in VB10. I was able to figure out how to make everything work except for the main element, the Database controls. Is there something that I can read to learn how the new code works?
Thanks, Garner
|
|
|
|
|
When you say Database controls, are you talking about UI or at a data access level. An example code snippet would also help answer your question.
"You get that on the big jobs."
|
|
|
|
|
Now I think I’m worse off than I originally thought since I don’t even know how to answer your question. Guessing “UI” means “User Interface?” If so, I think that is where my problem is.
Here’s the old VB6 code I used for my “Update” button in the user interface. VB10 highlights just about all of it with all kinds of errors. The worst part is that I have several buttons in each of several interfaces controlling several databases. “Add,” “Update,” “Delete,” & “Refresh” are the buttons that I am using.
Private Sub cmdUpdate_Click()<br />
On Error GoTo UpdateErr<br />
<br />
Dim db As Database<br />
Set db = OpenDatabase("c:\microsoft visual studio\vb98\openaccts.mdb")<br />
<br />
Dim rs As Recordset<br />
Set rs = db.OpenRecordset("prices", dbOpenDynaset)<br />
<br />
'Edit record and show new prices.<br />
<br />
rs.Edit<br />
<br />
rs.Fields("1b") = txtFields(0)<br />
rs.Fields("2b") = txtFields(1)<br />
rs.Fields("2r") = txtFields(2)<br />
rs.Fields("3a") = txtFields(3)<br />
rs.Fields("3s") = txtFields(4)<br />
rs.Fields("as") = txtFields(5)<br />
rs.Fields("br") = txtFields(6)<br />
rs.Fields("bs") = txtFields(7)<br />
rs.Fields("fd") = txtFields(8)<br />
rs.Fields("ms") = txtFields(9)<br />
rs.Fields("os") = txtFields(10)<br />
rs.Fields("ts") = txtFields(11)<br />
rs.Fields("tu") = txtFields(12)<br />
rs.Fields("ws") = txtFields(13)<br />
<br />
rs.Update<br />
<br />
'Change prices on PRODUCT form.<br />
<br />
frmprdm!txt1b.text = rs.Fields("1b")<br />
frmprdm!txt2b.text = rs.Fields("2b")<br />
frmprdm!txt3a.text = rs.Fields("3a")<br />
frmprdm!txt3u.text = rs.Fields("3s")<br />
frmprdm!txtas.text = rs.Fields("as")<br />
frmprdm!txtbr.text = rs.Fields("br")<br />
frmprdm!txtbs.text = rs.Fields("bs")<br />
frmprdm!txtfd.text = rs.Fields("fd")<br />
frmprdm!txtms.text = rs.Fields("ms")<br />
frmprdm!txtou.text = rs.Fields("os")<br />
frmprdm!txtts.text = rs.Fields("ts")<br />
frmprdm!txttu.text = rs.Fields("tu")<br />
frmprdm!txtws.text = rs.Fields("ws")<br />
<br />
Exit Sub<br />
UpdateErr:<br />
MsgBox Err.Description<br />
End Sub
I have read a ton of information online about changing the code and; so far, none of it has worked for me.
Thanks, Garner
|
|
|
|
|
Read up on DataTables, DataSets and DataRows - that should help.
|
|
|
|
|
As mentioned ADO.NET is different to ADO but looking at that code I would recommend you may do well to use Entity Framework. It will abstract away a lot of the implementation and should fit quite nicely with your current model. Julie Lerman has created a great beginners guide screencast. If you want to get up and running fast, I'd go straight to EF - Part 1 section.
Enjoy
"You get that on the big jobs."
|
|
|
|
|
Implementation of ADO.Net is different from ADO, so you will need to read about ADO.Net and learn it before using it.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
|
|
|
|
|
I pretty much figured out that everything was different when none of my code worked anymore and nothing that I did helped. That was pretty much my original question. What do I read and where do I find it? I have read stuff online for hours in forums, MSDN, sample code ...? I have looked for books on the subject but I haven't found anything that seems like it would help. In VB6 I went to the book store, picked up a book, "Visual Basic 6 Database Programming," and everything I needed was there. No such luck with VB10.
|
|
|
|
|
Here's a link to Amazon.com ADO.NET on Amazon[^]
Some in the list are specific for VB.
Julia Lerman's book is also mentioned and, like Rob above, I would recommend going the Entity Framework route as you then have an abstraction layer over the ADO.NET itself.
It’s not because things are difficult that we do not dare, it’s because we do not dare that things are difficult. ~Seneca
|
|
|
|
|
Thanks everyone for all of the information! Now I will have to see if my mind can absorb enough of it to make my code work. If not I know where to come for more help.
Thanks again, Garner
|
|
|
|
|
How to use WIA.dll to scan from adf in scaner
|
|
|
|