-
Feb 26th, 2022, 11:44 AM
#921
Re: Why is VB.Net/C#/XAML + VS2019 is better than VB6? Here's why.....
Strings are just reference types, so you are generally not swapping content.
My usual boring signature: Nothing
-
Feb 26th, 2022, 06:16 PM
#922
Re: Why is VB.Net/C#/XAML + VS2019 is better than VB6? Here's why.....
Originally Posted by wqweto
How do you swap strings without copying their content? Is it possible in .Net at all?
Actually Shaggy is 100% correct. .Net Strings are just like any other reference type. Assignments do not copy the entire internal state of a reference type, they only copy the references which is essentially akin to just copying their pointers. You can actually test it for yourself using this code:-
Code:
Imports System.Runtime.InteropServices
Imports System.Text
Public Class Form1
Private _r As New Random
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim s1 As String = CreateRandomString()
Dim s2 As String = CreateRandomString()
Dim s3 As String
Debug.WriteLine($"s1 = {s1}")
Debug.WriteLine($"s2 = {s2}")
'Print address of the first string
Debug.WriteLine($"Before swap : {StrPtr(s1)}")
'Swap the Strings
s3 = s1
s1 = s2
s2 = s3
'Print the address of the first string after it's
'been swapped to the second variable
Debug.WriteLine($"After swap : {StrPtr(s2)}")
'The String is passed by value to this version
'of StrPtr and even then, no copied is made
Debug.WriteLine($"StrPtr ByVal : {StrPtrByVal(s2)}")
Debug.WriteLine($"s1 = {s1}")
Debug.WriteLine($"s2 = {s2}")
End Sub
Private Function StrPtr(ByRef s As String) As IntPtr
Dim h = GCHandle.Alloc(s, GCHandleType.Pinned)
Try
Return h.AddrOfPinnedObject
Finally
h.Free()
End Try
End Function
Private Function StrPtrByVal(ByVal s As String) As IntPtr
Dim h = GCHandle.Alloc(s, GCHandleType.Pinned)
Try
Return h.AddrOfPinnedObject
Finally
h.Free()
End Try
End Function
'We create a String at runtime just to be absolutely
'certain the compiler isn't performing any compile time
'optimizations
Private Function CreateRandomString() As String
Dim sb As New StringBuilder
For i = 1 To 4
sb.Append(ChrW(_r.Next(97, 123)))
Next
Return sb.ToString
End Function
End Class
Output:-
Code:
s1 = atcx
s2 = cblr
Before swap : 46686984
After swap : 46686984
StrPtr ByVal : 46686984
s1 = cblr
s2 = atcx
As you can see, after the swap the String pointers remain the same. The String buffers were not copied or tampered with in any way. Even when Strings are passed by value in VB.Net, they aren't copied. Only the references are copied.
Originally Posted by wqweto
Btw, the VB6 sample code does *not* even copy strings because it swaps pointers which is an optimization of the straight-forward VB6 solution....
As proven above, Strings aren't copied in VB.Net either. However, I still went ahead and did some testing to see if there were any performance implications due to the subtle differences between references in .Net and raw pointers in VB6. See the next post.
Last edited by Niya; Feb 26th, 2022 at 06:29 PM.
-
Feb 26th, 2022, 07:22 PM
#923
Re: Why is VB.Net/C#/XAML + VS2019 is better than VB6? Here's why.....
Originally Posted by wqweto
Btw, the VB6 sample code does *not* even copy strings because it swaps pointers which is an optimization of the straight-forward VB6 solution.
I decided to test Elroy's pointer swapping shuffle algorithm to an analogous reference swapping algorithm in VB.Net. I tested both algorithms against an array of 10 million strings and the results were surprising. But first here are the algorithms:-
VB6
Code:
Option Explicit
Private Declare Function GetMem4 Lib "msvbvm60" (ByRef Source As Any, ByRef Dest As Any) As Long ' Always ignore the returned value, it's useless.
Private Sub Form_Load()
Dim sa() As String
Dim t As Double
Dim t1 As Double
sa = CreateStrArray
t = Timer
StrArrShuffle sa
t1 = Timer - t
Me.Text1.Text = CStr(t1)
Debug.Print CStr(t1)
End Sub
Public Sub StrArrShuffle(sa() As String)
Dim i As Long, j As Long, k As Long
Randomize
For i = LBound(sa) To UBound(sa)
j = Int((UBound(sa) + 1) * Rnd)
GetMem4 ByVal VarPtr(sa(i)), k
GetMem4 ByVal VarPtr(sa(j)), ByVal VarPtr(sa(i))
GetMem4 k, ByVal VarPtr(sa(j))
Next
End Sub
Private Function CreateStrArray() As String()
Const MAX As Long = (10 ^ 6) * 10
Dim ar(0 To MAX - 1) As String
Dim i As Long
For i = 0 To MAX - 1
ar(i) = CStr(i)
Next
CreateStrArray = ar
End Function
VB.Net
Code:
Imports System.Text
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim max As Integer = (10 ^ 6) * 10
Dim sw As New Stopwatch
Dim r As New Random
Dim stringList = (From num In Enumerable.Range(0, max) Select CStr(num)).ToArray
sw.Restart()
StrArrShuffle(stringList)
sw.Stop()
Debug.WriteLine(sw.Elapsed.TotalSeconds.ToString)
End Sub
Public Sub StrArrShuffle(sa As String())
Dim r As New Random
Dim s As String, j As Integer
For i As Integer = 0 To sa.Length - 1
j = r.Next(0, sa.Length)
s = sa(i)
sa(i) = sa(j)
sa(j) = s
Next
End Sub
End Class
And the results on my PC:-
Code:
VB6 in IDE = 2.42899999999698
VB6 EXE with all optimizations enabled = 1.98200000000652
VB.Net IDE 32 bit debug build(no optimizations) = 1.3523047
VB.Net IDE 64 bit debug build(no optimizations) = 0.795997
If I'm being 100% honest, I expected at least the VB6 EXE with full optimization to have the best performance by far but it seems the 64 bit build of the .Net version performs the best. .Net performed better than the VB6 regardless of the types of builds involved on both sides.
Also, my testing further revealed that optimized release builds of the .Net version yielded no discernable difference in performance from unoptimized debug builds.
-
Feb 26th, 2022, 10:27 PM
#924
Re: Why is VB.Net/C#/XAML + VS2019 is better than VB6? Here's why.....
Also on the topic of conciseness, just for fun I went for the most concise version possible:-
Code:
Private Sub ShuffleDemo()
Dim r As New Random
Debug.WriteLine(String.Join("|", From s In {"red", "green", "blue", "yellow", "purple"}
Order By r.Next))
End Sub
Pretty neat if you need to shuffle around a hardcoded array for any reason.
-
Feb 27th, 2022, 08:16 AM
#925
Re: Why is VB.Net/C#/XAML + VS2019 is better than VB6? Here's why.....
I curse this thread and the Necromancer that brought it back to life.
https://github.com/yereverluvinunclebert
Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.
By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.
-
Feb 27th, 2022, 11:23 AM
#926
Re: Why is VB.Net/C#/XAML + VS2019 is better than VB6? Here's why.....
It was cursed before you got here.
My usual boring signature: Nothing
-
Feb 27th, 2022, 01:35 PM
#927
Re: Why is VB.Net/C#/XAML + VS2019 is better than VB6? Here's why.....
Originally Posted by Niya
VB.Net
Code:
Imports System.Text
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim max As Integer = (10 ^ 6) * 10
Dim sw As New Stopwatch
Dim r As New Random
Dim stringList = (From num In Enumerable.Range(0, max) Select CStr(num)).ToArray
sw.Restart()
StrArrShuffle(stringList)
sw.Stop()
Debug.WriteLine(sw.Elapsed.TotalSeconds.ToString)
End Sub
Public Sub StrArrShuffle(sa As String())
Dim r As New Random
Dim s As String, j As Integer
For i As Integer = 0 To sa.Length - 1
j = r.Next(0, sa.Length)
s = sa(i)
sa(i) = sa(j)
sa(j) = s
Next
End Sub
End Class
... I expected at least the VB6 EXE with full optimization to have the best performance by far ...
It's the Rnd-call, as well as the GetMem4-functioncalls in the inner-loop, which slows VB6 down...
A VB6-pointer-swap-version with much better performance (0.33sec native, 0.77sec in IDE), is this one here:
Code:
Private Declare Function ArrPtrV& Lib "msvbvm60" Alias "__vbaRefVarAry" (Arr)
Private Declare Function GetMem4& Lib "msvbvm60" (Src As Any, Dst As Any)
Private Sub Form_Load()
ReDim sa(0 To 10 ^ 7 - 1) As String
Dim i As Long, T!
For i = 0 To UBound(sa): sa(i) = i: Next
T = Timer
StrArrShuffle sa
Caption = Timer - T
End Sub
Public Sub StrArrShuffle(sa() As String, Optional ByVal Seed# = 327680)
Const a& = 1140671485, c& = 12820163, z& = 2 ^ 24 'VB6-LCG-Consts
Dim i&, j&, p&, pa&(), L&: L = UBound(sa) + 1
GetMem4 ByVal ArrPtrV(sa), ByVal ArrPtrV(pa)
For i = 0 To L - 1
Seed = (Seed * a + c) / z: Seed = Seed - Int(Seed)
j = Int(L * Seed)
p = pa(i): pa(i) = pa(j): pa(j) = p 'pointer-swap
Seed = Seed * z
Next
GetMem4 0&, ByVal ArrPtrV(pa)
End Sub
HTH
Olaf
-
Feb 27th, 2022, 02:27 PM
#928
Re: Why is VB.Net/C#/XAML + VS2019 is better than VB6? Here's why.....
Originally Posted by Schmidt
It's the Rnd-call, as well as the GetMem4-functioncalls in the inner-loop, which slows VB6 down...
A VB6-pointer-swap-version with much better performance (0.33sec native, 0.77sec in IDE), is this one here:
Code:
Private Declare Function ArrPtrV& Lib "msvbvm60" Alias "__vbaRefVarAry" (Arr)
Private Declare Function GetMem4& Lib "msvbvm60" (Src As Any, Dst As Any)
Private Sub Form_Load()
ReDim sa(0 To 10 ^ 7 - 1) As String
Dim i As Long, T!
For i = 0 To UBound(sa): sa(i) = i: Next
T = Timer
StrArrShuffle sa
Caption = Timer - T
End Sub
Public Sub StrArrShuffle(sa() As String, Optional ByVal Seed# = 327680)
Const a& = 1140671485, c& = 12820163, z& = 2 ^ 24 'VB6-LCG-Consts
Dim i&, j&, p&, pa&(), L&: L = UBound(sa) + 1
GetMem4 ByVal ArrPtrV(sa), ByVal ArrPtrV(pa)
For i = 0 To L - 1
Seed = (Seed * a + c) / z: Seed = Seed - Int(Seed)
j = Int(L * Seed)
p = pa(i): pa(i) = pa(j): pa(j) = p 'pointer-swap
Seed = Seed * z
Next
GetMem4 0&, ByVal ArrPtrV(pa)
End Sub
HTH
Olaf
Ah ok.
On my PC it's 0.99 seconds in the IDE and 0.48 seconds as a fully optimized native binary. This makes more sense. .Net references sit at a higher level of abstraction than raw pointers so it didn't make sense to me why the .Net version was faster.
-
Feb 27th, 2022, 03:23 PM
#929
Re: Why is VB.Net/C#/XAML + VS2019 is better than VB6? Here's why.....
Doesn't that ultimately just show that non-optimized code is slower than optimized code, largely regardless of the language?
My usual boring signature: Nothing
-
Feb 27th, 2022, 03:35 PM
#930
Re: Why is VB.Net/C#/XAML + VS2019 is better than VB6? Here's why.....
Let me expand on that criticism a bit, because it's relevant to this whole nutty discussion:
This thread got resurrected to show a concise, yet inefficient .NET solution. If you look back to post #915, that's what it was: Concise, with no regard for speed. That morphed to a discussion of speed without regard for lines of code in VB.NET compared against a concise, but inefficient VB6. And now, concise has gone out the window for a discussion of how fast you can do something in two different languages as long as you put magical sideboards on the discussion.
The sideboards are magical because they change for every post. Suppose you were to write the tightest possible ASM method. This could be called from either language. At that point, what are you even measuring? Those last few posts are getting mighty close to that point, which is a far distance from a one line LINQ shuffle in VB.NET vs a naïve shuffle in VB6. What's the endpoint? Why spend time at these intermediate points along the way?
My usual boring signature: Nothing
-
Feb 27th, 2022, 08:35 PM
#931
Re: Why is VB.Net/C#/XAML + VS2019 is better than VB6? Here's why.....
Originally Posted by Shaggy Hiker
If you look back to post #915, that's what it was: Concise, with no regard for speed.
And as shown in post #917, the VB6-version is in no way "less concise" (though disregarding speed as well).
I think according to the title of this thread - that Niyas goal here was (not sure if it still is...),
to demonstrate "a higher efficiency regarding ease of implementation in .NET".
But as with all the other examples he brought before, also #915 vs. #917 clearly show,
that there is no such difference.
Originally Posted by Shaggy Hiker
Suppose you were to write the tightest possible ASM method...
Why spend time at these intermediate points along the way?
Not sure, why you're having such a hard time, understanding the purpose of "speed-optimized examples".
(and BTW - as shown in #927... the implementation-efforts are not different among the languages here either).
Speed-optimized examples (as in #927) are the "Functions you write, when you plan to put them behind a lib".
Whereas #915 or #917 are examples for, how "normal users" implement, whilst solving their daily problems - using existing libs.
Lib-authors want to export "decently fast, reusable functions" (if possible without "going all the way down to ASM) -
and normal devs want to use simple (low LOC requiring) patterns and lib-methods, to solve everyday-problems.
Good thing when a given programming-language is flexible (and fast) enough, to serve "both user-groups".
Neither user-group wants to go "out of their way" (or invest an indecent amount of time) in their implementation-efforts.
I always thought, that such code-comparisons - no matter if written under a "speed-", or "as short as possible"-aspect,
are educational for most people (how to implement stuff efficiently for different scenarios)...
Olaf
-
Feb 28th, 2022, 02:18 AM
#932
Re: Why is VB.Net/C#/XAML + VS2019 is better than VB6? Here's why.....
I find this thread very educational and helpful in reducing "animosity" to VB.Net
Both languages struggle but find ways to deal with daily challenges.
cheers,
</wqw>
-
Feb 28th, 2022, 12:13 PM
#933
Re: Why is VB.Net/C#/XAML + VS2019 is better than VB6? Here's why.....
I also find this thread interesting. I think people should be able to discuss whatever they want. However, I'm just thrilled that this thread isn't over in the main VB6 Q&A forum. It's easily ignorable over here.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Feb 28th, 2022, 02:55 PM
#934
Re: Why is VB.Net/C#/XAML + VS2019 is better than VB6? Here's why.....
Honestly, I actually like this thread, especially when there's a lot of comparative code out there.
Comparing the differences between two programming languages in code is welcome in any technical forum.
-
Mar 1st, 2022, 07:14 PM
#935
Re: Why is VB.Net/C#/XAML + VS2019 is better than VB6? Here's why.....
Originally Posted by Niya
Visual Studio 6 Intellisense
2 extra key presses to activate.
Visual Studio 2019 Intellisense
0 extra key presses to activate.
*mic drop*
Come here... Axtools or CodeSmart doesn't have an option to improve Intellisense, or I'm mistaken.
-
Mar 1st, 2022, 07:19 PM
#936
Re: Why is VB.Net/C#/XAML + VS2019 is better than VB6? Here's why.....
Originally Posted by Niya
If I'm being 100% honest, I expected at least the VB6 EXE with full optimization to have the best performance by far but it seems the 64 bit build of the .Net version performs the best. .Net performed better than the VB6 regardless of the types of builds involved on both sides.
I remember doing a little algorithm in VB.Net and VB6. VB.Net ran faster yes...
-
Mar 1st, 2022, 08:18 PM
#937
Re: Why is VB.Net/C#/XAML + VS2019 is better than VB6? Here's why.....
Originally Posted by Episcopal
I remember doing a little algorithm in VB.Net and VB6. VB.Net ran faster yes...
As long as you don't post both algorithms (as VB.NET and VB-code) here,
I guess we have to take this as "a myth"...
Olaf
-
Mar 1st, 2022, 08:26 PM
#938
Re: Why is VB.Net/C#/XAML + VS2019 is better than VB6? Here's why.....
Originally Posted by Schmidt
As long as you don't post both algorithms (as VB.NET and VB-code) here,
I guess we have to take this as "a myth"...
Olaf
You're right Olaf.... I lost the code, but I saw it on another forum.... More or less was this code.
Code:
Dim iLoop as Long
Dim X as long
Dim Temp as Double
Temp = Timer
For iLoop = 1 to 10000000
X = GetValue(X)
Next
Msgbox Timer - Temp
Private Function GetValue(byval Value as long) as Long
GetValue = Value +1
End Function
I don't know how to translate this to VB.NET anymore.
-
Mar 1st, 2022, 08:58 PM
#939
Re: Why is VB.Net/C#/XAML + VS2019 is better than VB6? Here's why.....
For something as simple as that, the difference in performance could just have to do with Long vs Integer.
My usual boring signature: Nothing
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|