String connection is an operation that is frequently used during development.
The purpose of this comparison is to look at the improvement of. Net in string connection and the efficiency of full compilation?
Test using three development tools: VB6, VB. net2003 (because C # is the same as VB. NET, so there is no test), Delphi7
Test string Hello world! You are welcome! The time consumed by 50000 connections.
Use API _ gettickcount to calculate the consumption. Testing Machine: Sai Yang 1g, memory: 390, win2000
Test 1: VB (select all the optimized compilation options)
Because VB uses a common string for connection efficiency and is low, the time consumed by using cstringbuilder for connection is used in this test.
Common connection code
Dim II as long
Dim STR as string
Dim t as long
T = gettickcount
For II = 1 to 50000
STR = STR & "Hello world! You are welcome! "
Next
Msgbox CSTR (gettickcount-T)
Test results: no response within 2 minutes... failed
Use cstringbuilder to connect to the code
Dim II as long
Dim STR as new cstringbuilder
Dim t as long
Dim ss as string
T = gettickcount
For II = 1 to 50000
Str. APPEND "Hello world! You are welcome! "
Next
Ss = Str. tostring
Msgbox CSTR (gettickcount-T)
Test results: test1: 521, Test2: 511, test3: 531
Median: 521
2. VB. NET Testing
In VB. NET, although & can still be used to connect strings, the efficiency of the method in this method is not improved.
The system. Text. stringbuilder class is connected
Common connection code:
Dim II as int32
Dim STR as string
Dim ss as string
Dim t as int32
T = gettickcount ()
For II = 1 to 50000
STR = STR & "Hello world! You are welcome! "
Next
MessageBox. Show (CSTR (gettickcount-t ))
Test results: no response within 2 minutes, failed
Use System. Text. stringbuilder to connect and Test
Dim II as int32
Dim STR as new system. Text. stringbuilder
Dim ss as string
Dim t as int32
T = gettickcount ()
For II = 1 to 50000
Str. append ("Hello world! You are welcome! ")
Next
Ss = Str. tostring
MessageBox. Show (CSTR (gettickcount-t ))
Test results: test1: 60, Test2: 60, test3: 60
Median: 60
3. Delphi
In Delphi, + is used to connect strings. The difference is that Borland has fully optimized the connection method.
VaR T, II: integer;
VaR STR: string;
VaR strtime: string;
VaR SS: string;
Begin
T: = gettickcount; // use api_gettickcount to be compatible with all development programs
For II: = 1 to 50000 do
Begin
STR: = STR + 'Hello world! You are welcome! '; // String connection
End;
SS: = STR; // send a string
Strtime: = inttostr (gettickcount-T); // obtain the usage time
MessageBox (0, pchar (strtime), '', mb_iconinformation );
End;
Test results: test1: 20, Test2: 30, test3: 20
Median: 20
Conclusion of the three tools:
In fact, the principle of string optimization is: the string connection always destroys the original object or connects to the original object.
There is no doubt that Delphi's string connection speed is the fastest. It seems Borland has made some effort in the compiler.
However,. Net does not have any optimization in the way of using common connections, while stringbuilder's VB and VB.net are used for speed.
There has been a huge improvement, which can be described as a hundred times. Because VB.net itself is fully compiled, the speed difference with VB is
60: 521... although. NET is not as fast as Delphi, but the result is very close, just a few milliseconds difference, basically negligible
In fact, most of Microsoft's development tools have very low efficiency in using simple string connection, because Microsoft has not optimized it.
Of course, except VC ++, because it is connected by a string array and a parallel method, of course, it provides a similar
Stringbulider cstring object. It is estimated that the operation method is similar to that of stringbuilder. Because of my poor C skill, no test is conducted.
If you are interested, you can perform the above tests by yourself, including VC and post the results to this post for your reference.
//////////////////////////////
Appendix: For the code usage of the cstringbuilder class used in VB6, refer to the above
Option explicit
'The secret to this class is that it uses the join
'Function which is part of the VBA. Strings class
Private mvarstringarray () as string
Private mvararrayitems as long
Public sub append (byval newstr as string)
Redim preserve mvarstringarray (mvararrayitems) as string
Mvarstringarray (mvararrayitems) = newstr
Mvararrayitems = mvararrayitems + 1
End sub
Public property get tostring () as string
If mvararrayitems> 0 then tostring = join (mvarstringarray ,"")
End Property
Public sub reset ()
Mvararrayitems = 0
Erase mvarstringarray
End sub
Private sub class_initialize ()
If mvararrayitems> 0 then reset
End sub
Private sub class_terminate ()
Reset
End sub