Site
News
Files Visual Basic
Strings
Math
General
Properties
Memory
Methods Search
Testing Inline ASM-VB
Strings
Math
General
Memory Search
Using inline ASM
Submit!
News
Files Visual Basic
Strings
Math
General
Properties
Memory
Methods Search
Testing Inline ASM-VB
Strings
Math
General
Memory Search
Using inline ASM
Submit!
'With' vs.' No With'The results of this test are very irregular.
Sometimes "With" wins and some "No with" wins. I've done this test 10 times, but everytime the same results...
I'm not sure why this happens. I always thought that "With" would be (much) faster anyway.
It could be the form I've used for this test, but it's highly unlikely.
For this test I've done 5000 iterations, and done the test 5 times. Changing properties take so much time in VB.
I have limitted the form calls to properties that are not graphical depending on something.
Used code:
User contributed notes:
Code: |
'No With' % faster than 'With' | 'No With' (sec) | 'With' (sec) |
-0,1% | 0,498539 | 0,498938 |
0,9% | 0,496477 | 0,492082 |
-1,1% | 0,492939 | 0,498628 |
-2,5% | 0,487473 | 0,499922 |
8,6% | 0,532967 | 0,490634 |
User contributed notes:
Author: Jeff Beem () | Date: 20:04 15/04/2005 |
I haven't tested it but I'm certain you will see more of a difference if you use 'With' on an object property of another object. For example: With TreeView1.Nodes .Add ... End With as opposed to Treeview1.Nodes.Add ... Treeview1.Nodes.Add ... The reason is because in the first example the Nodes property is accessed only once where in the second example it's accessed each time. Test that and my guess is you'll see more of a difference between the two methods. |
|
Author: Tom (hurendo_kun at hotmail dot com) | Date: 15:05 31/05/2005 |
There shouldn't be any difference at all. The code generated is identical either way. The differential in your test results --- which I would consider negligible anyway --- is undoubtedly bumps in your operating system. | |
Author: Sylvain Perez () | Date: 14:09 08/09/2005 |
I do confirm what Jeff Beem wrote, and that your test seems not to be the good one. 1) You have to compare on a few properties, like 2 or 3. Because at a certain point (I mesured approx 6 to 8), With or without With is the same speed. But for say 2 properties inside the With, the without version is definitely FASTER (but read 2). Of course, the opposite is true : if you access 20 properties, than With is now FASTER then without (but read 2). 2) It also depends a lot of the "level" of the properties. The level 1 corresponds to what I wrote in 1). Like "Treeview1.Nodes" : for a few properties, the without is faster than the with. At level 2 (and further), like "Treeview1.Nodes.Add" then the With version is always faster, almost whatever the number of properties inside. So for a few month I realized making my app faster using these rules : * level 2 or more : always With * level 1 : if approx less than 6 inside, I do "without", if more, i do "with". If it can help... |
|
Author: Sylvain Perez () | Date: 14:09 08/09/2005 |
I resume as an example, my english is not perfect... SLOW VERSION : With ThisThing .GetSomething .GetSomething End With MUCH FASTER VERSION : ThisThing.GetSomething ThisThing.GetSomething BUT, THIS IS FAST With ThisThing .GetSomething .GetSomething .GetSomething .GetSomething .GetSomething .GetSomething End With AND THIS IS SLOWER (qty may differ a bit) ! ThisThing.GetSomething ThisThing.GetSomething ThisThing.GetSomething ThisThing.GetSomething ThisThing.GetSomething ThisThing.GetSomething BUT THIS IS ALWAYS FASTER (from level 2 to more, for any qty) : With ThisThing.AtLevel2 .GetSomethingElse .GetSomethingElse End With |
|
Author: Andrew 'SuperDre' Jakobs () | Date: 17:09 22/09/2005 |
But isn't with a lot faster when used with arrays or collections? ex. MyObjects(100).GetSomeThing MyObjects(100).GetSomeThing MyObjects(100).GetSomeThing MyObjects(100).GetSomeThing with MyObjects(100) .GetSomeThing .GetSomeThing .GetSomeThing .GetSomeThing end with or would this be faster: MyObject = MyObjects(100) MyObject.GetSomeThing MyObject.GetSomeThing MyObject.GetSomeThing MyObject.GetSomeThing It all depends ofcourse what the compiler does.. if the compiler would 'adds' the MyObjects(100) to every line then the MyObject = would be MUCH faster as not every call has the 100 lookup.. |
|
Author: Tom (hurendo_kun at hotmail dot com) | Date: 05:09 23/09/2005 |
Hmm. Using With may invoke additional caching that isn't normally used, which would account for the speed difference. You'd have to look at the assembly listing to know for sure. | |
Author: Tom (hurendo_kun at hotmail dot com) | Date: 19:10 21/10/2005 |
I did some extra research on this. Here's where the difference is really visible: Document.Page.Line.Word(0).Char(Ubound(Document.Page.Line.Word(0).Char) = "A" As opposed to: With Document.Page.Line.Word(0) .Char(Ubound(.Char)) = "A" End With You should notice a HUGE difference when you start doing this a few thousand times. |
|
Author: Sylvain Perez () | Date: 03:11 19/11/2005 |
Yes, this seems obvious, and confirms what I wrote as: "At level 2 (and further), like 'Treeview1.Nodes.Add' then the With version is always faster, almost whatever the number of properties inside." |