JDYoder Posted July 27, 2005 Posted July 27, 2005 What are the differences? Pros and cons? When to use one over the other? Quote
mhildner Posted July 27, 2005 Posted July 27, 2005 This is a decent write up on that - http://www.developerfusion.co.uk/show/4341/7/ Two things I think are important with structures is speed - the 16 byte rule and that they're allocated on the stack, not the heap, like clases are. Mike Quote
inter Posted July 29, 2005 Posted July 29, 2005 What are the differences? Pros and cons? When to use one over the other? Well..... the difference between structures and classes is that instances of structures are VALUE types and classes are REFERENCE types. what does this mean in practice .... structure dim test as strnew dim test2 as strnew test.one = 5 test2.one = 6 test.one = test2.one 'test.one = 6 and test2.one = 6 test2.one = 8 'test.one = 6 and test2.one = 8 Classes dim test as classnew dim test2 as classnew test.one = 5 test2.one = 6 test.one = test2.one 'test.one = 6 and test2.one = 6 test2.one = 8 'test.one = 8 and test2.one = 8 !!! the reason this happens is because you have reference types when you work with classes.... these types refer to a memory location.... so you copy the reference and not the value (I haven't tested the code above...Correct me if I am wrong....but that should be the basic idea... structures - value types / classes - reference types) INTER Quote
BlackStone Posted July 29, 2005 Posted July 29, 2005 test.one = test2.one 'test.one = 6 and test2.one = 6 test2.one = 8 'test.one = 8 and test2.one = 8 !!! That wouldn't happen, because .one (which looks like an integer/short/etc.) is a value type, therefore test.one is stored in test and test2.one is stored in test2. If it was a reference type, such as a StringBuilder (though strings, which are reference types, behave like value types in this situation because they are immutable) then the behavior would be similar to what you showed above. In addition, classes must be initialized before use, the same is not true for structs. Quote "For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
Diesel Posted July 29, 2005 Posted July 29, 2005 In the article mhildner posted (http://www.developerfusion.co.uk/show/4341/7/), where did the author get the < 16 bytes range from? Structures are best suited for... "smaller data value types under 16 bytes - structures like numbers crunching" No background evidence given for the < 16 bytes. I think he made it up. Quote
mhildner Posted July 30, 2005 Posted July 30, 2005 (edited) No background evidence given for the < 16 bytes. I think he made it up.LOL. The first time I heard that I thought the same thing - why? You'll see the 16 byte rule mentioned over and over. My question is, why is that? Not that I have any evidence myself, I've never bothered to perf it out. Sometimes you hear something so many times from people that are smarter than you, you just take it for granted. Here's a better explanation, along with a disclaimer of course. Scroll down to "Data Size". Mike Edit: Allow me to point out that the above link mentions that there is a trade off between copying the bytes of a value (structure) and allocating a new reference on the heap (class). That does make sense. Edited July 30, 2005 by mhildner Quote
Talyrond Posted July 30, 2005 Posted July 30, 2005 Just a point of interest, performance is a real issue with my CAD system, I have spent many an our deliberating over what perform better. The day I tried out a performance profile (I use ANTS profiler) it was a revelation, I kid you not, with in two hours I saw a 15 fold increase in speed in a key area. You can hone in on bottle necks, you will be surprised what sort of thing cause performance issues. If you are serious about performance, you need a profiler. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.