|
-
Jul 24th, 2018, 01:02 AM
#1
Thread Starter
Addicted Member
Vb6 – late-binding with COM objects(flexgrid) 2 times slower
Code:
Public Function gridloop(MSFG1 As Object) As Long
For i= 0 To MSFG1.rows - 1
A = MSFG1.TextMatrix(i,1)
Next
End Function
The above code is 2 times slower than below
Code:
Public Function gridloop(MSFG1 As MSHFlexGrid) As Long
Public Function gridloop(MSFG1 As MSFlexGrid) As Long
Any solution to speed-up?
-
Jul 24th, 2018, 03:46 AM
#2
Re: Vb6 – late-binding with COM objects(flexgrid) 2 times slower
 Originally Posted by winman
Code:
Public Function gridloop(MSFG1 As Object) As Long
For i= 0 To MSFG1.rows - 1
A = MSFG1.TextMatrix(i,1)
Next
End Function
The above code is 2 times slower than below
Code:
Public Function gridloop(MSFG1 As MSHFlexGrid) As Long
Public Function gridloop(MSFG1 As MSFlexGrid) As Long
Any solution to speed-up?
When the compiler knows exactly what object types it is working with it can make a lot of optimisations, hence the large performance gains. Late bound objects cannot benefit from these optimisations as everything needs to be done at runtime.
The easiest solution is to always use early binding whenever possible. Is there a reason you need to support both MSHFlexGrid and MSFlexGrid in this piece of code? If performance is critical it might just be worth having duplicate code with each one early bound.
-
Jul 24th, 2018, 09:28 AM
#3
Re: Vb6 – late-binding with COM objects(flexgrid) 2 times slower
something like...
Code:
Public Function gridloop(FG As Object) As Long
If TypeOf FG Is MSHFlexGrid Then
Dim msh As MSHFlexGrid
Set msh = FG
With msh
For i = 0 To .rows - 1
a = .TextMatrix(i, 1)
Next
End With
ElseIf TypeOf FG Is MSFlexGrid Then
Dim msg As MSFlexGrid
Set msg = FG
With msg
For i = 0 To .rows - 1
a = .TextMatrix(i, 1)
Next
End With
End If
End Function
-
Jul 24th, 2018, 01:53 PM
#4
Re: Vb6 – late-binding with COM objects(flexgrid) 2 times slower
Most of the MSKB articles on this topic related to VB are gone now. The best place to find these things today is in the October 2001 MSDN Library CDs.
One of the few more general articles remaining on this topic may be Using early binding and late binding in Automation.
Early binding is the preferred method. It is the best performer because your application binds directly to the address of the function being called and there is no extra overhead in doing a run-time lookup. In terms of overall execution speed, it is at least twice as fast as late binding.
In the case you gave there is a simple answer: convert all usage of the old VB5 ANSI flexgrid control to the newer VB6 Unicode hierarchical flexgrid. Then your need for late binding disappears. You don't really want the overhead of deploying both libraries anyway and wasting memory on loading both is sort of silly.
Or if you insist on that then create two separate early-bound procedures, one for each control class.
-
Jul 25th, 2018, 05:23 AM
#5
Thread Starter
Addicted Member
Re: Vb6 – late-binding with COM objects(flexgrid) 2 times slower
There are thousands of procedures, so cant repeat the code.
since implementing MSHFlexGrid for first time, cant risk changing all grids in project
-
Jul 25th, 2018, 05:23 AM
#6
Thread Starter
Addicted Member
Re: Vb6 – late-binding with COM objects(flexgrid) 2 times slower
There are thousands of procedures, so cant repeat the code.
since implementing MSHFlexGrid for first time, cant risk changing all grids in project
-
Jul 25th, 2018, 06:21 AM
#7
Re: Vb6 – late-binding with COM objects(flexgrid) 2 times slower
Hi,
why don't you show what you are doing with the MS(H)Flexgrid
Load / Save / Replace Text ????
here somthing to play with...
Code:
Option Explicit
Private Sub Command1_Click()
Dim s() As String
Dim i As Long, j As Long
s() = FlexgridGetSelectedCellsArray(MSFlexGrid1)
For i = LBound(s, 1) To UBound(s, 1)
For j = LBound(s, 2) To UBound(s, 2)
Debug.Print s(i, j) & vbTab;
'Replace something
s(i, j) = Replace(s(i, j), "Cell", "MyCell")
Next
' Debug.Print
Next
FlexgridPutSelectedCellsArray MSFlexGrid1, s()
Debug.Print FlexgridGetSelectedCellsString(MSFlexGrid1)
End Sub
Private Sub Command2_Click()
Dim s() As String, s1 As String
Dim i As Long, j As Long
s() = FlexgridGetSelectedCellsArray(MSHFlexGrid1)
For i = LBound(s, 1) To UBound(s, 1)
For j = LBound(s, 2) To UBound(s, 2)
Debug.Print s(i, j) & vbTab;
Next
Debug.Print
Next
s1 = FlexgridGetSelectedCellsString(MSHFlexGrid1)
Debug.Print s1
'Replace something
s1 = Replace(s1, "Cell", "MSHCELL")
FlexgridPutSelectedCellsString MSHFlexGrid1, s1
End Sub
Private Sub Form_Load()
Dim i As Long, j As Long
With MSFlexGrid1
.Rows = 20
.Cols = 6
.FillStyle = flexFillRepeat
For i = .FixedRows To .Rows - 1
For j = .FixedCols To .Cols - 1
.TextMatrix(i, j) = "Cell " & i & "." & j
Next
Next
End With
With MSHFlexGrid1
.Rows = 20
.Cols = 6
For i = .FixedRows To .Rows - 1
For j = .FixedCols To .Cols - 1
.TextMatrix(i, j) = "Cell " & i & "." & j
Next
Next
End With
End Sub
'liefert den Inhalt markierter Cells eines Flexgrids
'als 2-dimensionales Array, Row und Col über Lbound
Public Function FlexgridGetSelectedCellsArray(FlexOrHFlex As Object) As String()
Dim Row As Long, RowSel As Long
Dim Col As Long, ColSel As Long
Dim i As Long, j As Long
Dim s() As String
'Reihenfolge feststellen
FlexgridGetSelectedCells FlexOrHFlex, Row, RowSel, Col, ColSel
With FlexOrHFlex
ReDim s(Row To RowSel, Col To ColSel)
For i = LBound(s, 1) To UBound(s, 1)
For j = LBound(s, 2) To UBound(s, 2)
s(i, j) = .TextMatrix(i, j)
Next
Next
End With
FlexgridGetSelectedCellsArray = s()
End Function
'liefert den Inhalt markierter Cells eines Flexgrids
'als String, Rows separated by vbCr, Cols by vbTab
Public Function FlexgridGetSelectedCellsString(FlexOrHFlex As Object) As String
Dim s() As String, s1() As String, s2() As String
Dim i As Long, j As Long
s() = FlexgridGetSelectedCellsArray(FlexOrHFlex)
ReDim s1(LBound(s, 1) To UBound(s, 1))
For i = LBound(s, 1) To UBound(s, 1)
ReDim s2(LBound(s, 2) To UBound(s, 2))
For j = LBound(s, 2) To UBound(s, 2)
s2(j) = s(i, j)
Next
s1(i) = Join(s2, vbTab)
Next
FlexgridGetSelectedCellsString = Join(s1, vbCr)
End Function
'schreibt den Inhalt eines 2-dimensionalen Arrays in ein Flex
'LBound(s, 1) entspricht .Row, Lbound(s, 2) entpricht .Col
Public Sub FlexgridPutSelectedCellsArray(FlexOrHFlex As Object, sArray() As String)
Dim Row As Long, Col As Long
For Row = LBound(sArray, 1) To UBound(sArray, 1)
For Col = LBound(sArray, 2) To UBound(sArray, 2)
FlexOrHFlex.TextMatrix(Row, Col) = sArray(Row, Col)
Next
Next
End Sub
'schreibt den Inhalt eines String in ein Flex
'String ist RowSeparated by vbCr, ColSeparated by vbTab
Public Sub FlexgridPutSelectedCellsString(FlexOrHFlex As Object, sString As String)
Dim Row As Long, Col As Long
Dim RowSel As Long, ColSel As Long
Dim i As Long, j As Long
Dim s() As String, s1() As String
Dim sArray() As String
FlexgridGetSelectedCells FlexOrHFlex, Row, RowSel, Col, ColSel
ReDim sArray(Row To RowSel, Col To ColSel)
s() = Split(sString, vbCr)
For i = LBound(s) To UBound(s)
s1() = Split(s(i), vbTab)
For j = LBound(s1) To UBound(s1)
sArray(Row + i, Col + j) = s1(j)
Next
Next
For Row = LBound(sArray, 1) To UBound(sArray, 1)
For Col = LBound(sArray, 2) To UBound(sArray, 2)
FlexOrHFlex.TextMatrix(Row, Col) = sArray(Row, Col)
Next
Next
End Sub
'liefert Row, RowSel, Col, ColSel in aufsteigender Reihenfolge
Public Sub FlexgridGetSelectedCells(FlexOrHFlex As Object, _
Row As Long, RowSel As Long, _
Col As Long, ColSel As Long)
With FlexOrHFlex
If .Row <= .RowSel Then
Row = .Row
RowSel = .RowSel
Else
Row = .RowSel
RowSel = .Row
End If
If .Col <= .ColSel Then
Col = .Col
ColSel = .ColSel
Else
Col = .ColSel
ColSel = .Col
End If
End With
End Sub
regards
Chris
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
Tags for this Thread
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
|