Dim w As Long
Dim p As Long
Dim c As New Scripting.Dictionary
c.Add "QQ", 1
For w = 1 To 10000000
p = c("QQ")
Next
以上程式 For....Loop 这段 code
VB6 = 2秒
VB.NET = 14秒
请问 VB.Net 慢很多原因是什么
有什方法让它变快呢
(1) VB6 的 String 是 OLE标准的BSTR
VB.Net 的 String 如果不是和 VB6 一样
那这样调用 c("QQ") 时就要多一道转换成 BSTR 过程 <---- 是这原因吗? 还是另有其他原因 ?
------解决方案--------------------
这样你的代码就快了10倍。。。
Imports System.Collections.Generic
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim c As New Scripting.Dictionary
' Dim c As New Dictionary(Of String, Long)
Dim w As Long
Dim t As Double
Dim p As Long
t = Microsoft.VisualBasic.Timer
c.Add("QQ", 1)
For w = 1 To 10000000
p = c("QQ")
Next
MsgBox(CLng((Microsoft.VisualBasic.Timer - t) * 10000) / 10000, , "AAAA")
End Sub
End Class
vb6的代码移植到VB.NET上是最笨的办法。
最好的办法是把C#代码移植过来。
至于为什么,请自己翻译:
COM Interoperability is the feature of Microsoft .NET that allows managed .NET code to interact with unmanaged code using Microsoft's Component Object Model semantics.
This article is geared towards C# programmers who are familiar with developing COM components and familiar with the concept of an interface. I'll review some background on COM, explain how C# interacts with COM, and then show how to design .NET components to smoothly interact with COM.
For those die-hard COM experts, there will be some things in this article that are oversimplified, but the concepts, as presented, are the important points to know for those developers supplementing their COM code with .NET components.
Introduction
.NET Interfaces and Classes
The basis for accessing .NET objects either from other .NET code or from unmanaged code is the Class. A .NET class represents the encapsulation of the functionality (methods and properties) that the programmer wants to expose to other code. A .NET interface is the abstract declaration of the methods and properties that classes which implement the interface are expected to provide in their implementations. Declaring a .NET interface doesn't generate any code, and a .NET interface is not callable directly. But any class which implements ("inherits") the interface must provide the code that implements each of the methods and properties declared in the interface definition.
Microsoft realized that the very first version of .NET needed a way to work with the existing Windows technology used to develop applications over the past 8+ years: COM. With that in mind, Microsoft added support in the .NET runtime for interoperating with COM - simply called "COM Interop". The support goes both ways: .NET code can call COM components, and COM code can call .NET components.
Using the code
Steps to create a Managed .NET C# COM Object:
Open VS.NET2003->New Project->Visual C# Projects->Class Library.