简介
,在这篇文章中,我们将采取一个共同的中间语言(CIL)的外观,是由各种语言的编译器在Visual Studio 2005生成的。。NET Framework的是什么?
简洁的目的会去回答这个问题的答案如下:。NET框架是一个软件组件,可添加到Microsoft Windows操作系统(虽然有一些能够在这个地方工作在Linux和其他操作系统上运行)。的。NET Framework组成的集合类包含预编码解决方案,以常见的编程要求。是在一个管理程序的运行要求的软件环境,中运行的。NET框架开发的应用程序。这被称为公共语言运行库(CLR)的运行时环境。 CLR提供了一个外观应用程序的虚拟机,从而使程序员不需要考虑特定的CPU将执行该程序的功能。 CLR还提供其他重要的服务,如安全保证,内存管理和异常处理。
运行时,也加快了开发人员的生产力。例如,程序员可以在他们选择的开发语言写的应用程序,尚未充分利用的运行库,类库,和其他开发人员用其他语言编写的组件。任何选择以运行库为目标的编译器供应商可以这样做。这一目标的。NET Framework的语言编译器。NET Framework中提供该语言编写的现有代码的功能,大大缓解了现有应用程序的迁移过程。。NET Framework的最重要组成部分,是在公共语言基础结构,或CLI。 CLI的目的是提供一个应用程序开发语言无关的平台。为了澄清,CLI是一个规范,而不是实现,往往是与(CLR)的困惑
CLI由5个主要部分:通用类型系统(CTS)通用语言规范(CLS)通用中间语言(CIL)就在即时编译器(JIT)虚拟执行系统(VES)
的所有5个这些部件的详细讨论超出了本文的范围。不过,我觉得以下两个组件需要的一些解释
通用语言规范(CLS) - 是的,微软已经帮助编译器厂商提供的规格设置。这些规范规定的最低组的功能。NET语言必须有。 &
#160; 通用中间语言(CIL) - 是最低级的人类可读的通用语言基础结构和NET Framework中的编程语言。语言的目标。NET Framework的编译成字节码,这是组装成CIL。 CIL的类似于面向对象的汇编语言,完全是基于堆栈的。它是由虚拟机执行。 只要在即时编译器(JIT) - (JIT)编译的功能使所有托管代码运行在它的系统上执行的本机语言。同时,内存管理器中删除零碎内存的可能性,以进一步提高性能和增加内存位置的参考。 所有人生而平等。NET语言?
不管。NET编程语言使用,语言编译器的输出是CIL中相同的逻辑表示。在执行程序之前,CIL是编译为目标代码适合的机器上的程序执行。这最后的编译步骤通常是在程序调用的时刻进行的。NET Framework CLR组件,虽然可以手动在早期阶段进行。这个概念
以研究是否有实际上是一个由四个编译器,我在我的Visual Studio 2005版本所提供的四个主要语言开发了四个相同的应用程序(VB.NET,C#,C和J#)编译的CIL的差异。我也跟着在创建应用程序相同的步骤。我Quadrifurcated的应用程序,并投掷了一些不良的生活习惯。我会编译所有这些应用程序,然后看看他们的MSIL编译器。中的应用
我创建控制台应用程序,基本上包含了计时秒表多久的应用程序执行了。应用程序本身基本上包含一个循环,遍历一千倍,串联字符串。结果打印出来字符串串联到控制台,也可以打印出来的时间长短了执行。
我省略StringBuilder类的使用,在所有这些应用,这是故意的,因为这是一个测试,看看StringBuilder类的效率。如果你看了这篇文章,你可以看到StringBuilder类所提供的性能收益。"守则"VB.NET
C#中{C}J#Imports System.Diagnostics
Module Module1
Sub Main()
Dim strSomeString As String = ""
Dim sw As New Stopwatch
sw.Start()
For i As Integer = 0 To 1000
strSomeString += "Test "
Next
sw.Stop()
Console.WriteLine(strSomeString)
Console.WriteLine(sw.ElapsedMilliseconds)
Console.ReadLine()
End Sub
End Module
彗星package ConsoleJSharp;
import System.*;
import System.Diagnostics.*;
public class Program
{
public static void main(String[] args)
{
String strSomeString = "";
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 1000; i++)
{
strSomeString += "Test ";
}
sw.Stop();
Console.WriteLine(strSomeString);
Console.WriteLine(sw.get_ElapsedMilliseconds());
Console.ReadLine();
}
}
编译的CILVB.NET#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
using namespace System::Diagnostics;
int main(array<System::String ^> ^args)
{
String^ strSomeString = "";
Stopwatch^ sw = gcnew Stopwatch;
sw->Start();
for (int i = 0;i< 1000 ;i++)
{
strSomeString += "Test ";
}
sw->Stop();
Console::WriteLine(strSomeString);
Console::WriteLine(sw->ElapsedMilliseconds);
Console::ReadLine();
}
C#中.method public static void Main() cil managed
{
.entrypoint
.custom instance void
[mscorlib]
System.STAThreadAttribute::.ctor()=(01 00 00 00)
// Code size 83 (0x53)
.maxstack 2
.locals init (
[0] string strSomeString,
[1] class [System]System.Diagnostics.Stopwatch sw,
[2] int32 i, [3] int32 VB$CG$t_i4$S0
)
IL_0000: nop
IL_0001: ldstr ""
IL_0006: stloc.0
IL_0007: newobj instance void
[System]
System.Diagnostics.Stopwatch::.ctor()
IL_000c: stloc.1
IL_000d: ldloc.1
IL_000e: callvirt instance void
[System]
System.Diagnostics.Stopwatch::Start()
IL_0013: nop
IL_0014: ldc.i4.0
IL_0015: stloc.2
IL_0016: ldloc.0
IL_0017: ldstr "Test "
IL_001c: call string
[mscorlib]
System.String::Concat(string, string)
IL_0021: stloc.0
IL_0022: nop
IL_0023: ldloc.2
IL_0024: ldc.i4.1
IL_0025: add.ovf
IL_0026: stloc.2
IL_0027: ldloc.2
IL_0028: ldc.i4 0x3e8
IL_002d: stloc.3
IL_002e: ldloc.3
IL_002f: ble.s IL_0016
IL_0031: ldloc.1
IL_0032: callvirt instance void
[System]
System.Diagnostics.Stopwatch::Stop()
IL_0037: nop
IL_0038: ldloc.0
IL_0039: call void
[mscorlib]
System.Console::WriteLine(string)
IL_003e: nop
IL_003f: ldloc.1
IL_0040: callvirt instance int64
[System]
System.Diagnostics.Stopwatch::get_ElapsedMilliseconds()
IL_0045: call void
[mscorlib]
System.Console::WriteLine(int64)
IL_004a: nop
IL_004b: call string
[mscorlib]
System.Console::ReadLine()
IL_0050: pop
IL_0051: nop
IL_0052: ret
} // end of method Module1::Main
J#.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 87 (0x57)
.maxstack 2
.locals init (
[0] string strSomeString,
[1] class [System]System.Diagnostics.Stopwatch sw,
[2] int32 i,
[3] bool CS$4$0000
)
IL_0000: nop
IL_0001: ldstr ""
IL_0006: stloc.0
IL_0007: newobj instance void
[System]
System.Diagnostics.Stopwatch::.ctor()
IL_000c: stloc.1
IL_000d: ldloc.1
IL_000e: callvirt instance void
[System]
System.Diagnostics.Stopwatch::Start()
IL_0013: nop
IL_0014: ldc.i4.0
IL_0015: stloc.2
IL_0016: br.s IL_002a
IL_0018: nop
IL_0019: ldloc.0
IL_001a: ldstr "Test "
IL_001f: call string
[mscorlib]
System.String::Concat(string,string)
IL_0024: stloc.0
IL_0025: nop
IL_0026: ldloc.2
IL_0027: ldc.i4.1
IL_0028: add
IL_0029: stloc.2
IL_002a: ldloc.2
IL_002b: ldc.i4 0x3e8
IL_0030: clt
IL_0032: stloc.3
IL_0033: ldloc.3
IL_0034: brtrue.s IL_0018
IL_0036: ldloc.1
IL_0037: callvirt instance void
[System]
System.Diagnostics.Stopwatch::Stop()
IL_003c: nop
IL_003d: ldloc.0
IL_003e: call void
[mscorlib]
System.Console::WriteLine(string)
IL_0043: nop
IL_0044: ldloc.1
IL_0045: callvirt instance int64
[System]
System.Diagnostics.Stopwatch::get_ElapsedMilliseconds()
IL_004a: call void
[mscorlib]
System.Console::WriteLine(int64)
IL_004f: nop
IL_0050: call string
[mscorlib]
System.Console::ReadLine()
IL_0055: pop
IL_0056: ret
} // end of method Program::Main
彗星.method public hidebysig static void main(string[]args)cil managed
{
.entrypoint
// Code size 108 (0x6c)
.maxstack 2
.locals init
(
[0] string strSomeString,
[1] class [System]System.Diagnostics.Stopwatch sw,
[2] int32 i
)
IL_0000: ldtoken [vjslib]
com.ms.vjsharp.lang.ObjectImpl
IL_0005: call void
[mscorlib]
System.Runtime.CompilerServices.
RuntimeHelpers::RunClassConstructor
(
valuetype [mscorlib]System.RuntimeTypeHandle
)
IL_000a: ldstr ""
IL_000f: stloc.0
IL_0010: newobj instance void
[System]
System.Diagnostics.Stopwatch::.ctor()
IL_0015: stloc.1
IL_0016: ldloc.1
IL_0017: callvirt instance void
[System]
System.Diagnostics.Stopwatch::Start()
IL_001c: ldc.i4.0
IL_001d: stloc.2
IL_001e: br.s IL_003f
IL_0020: newobj instance void
[vjslib]
java.lang.StringBuffer::.ctor()
IL_0025: ldloc.0
IL_0026: callvirt instance class
[vjslib]
java.lang.StringBuffer
[vjslib]
java.lang.StringBuffer::append(string)
IL_002b: ldstr "Test "
IL_0030: callvirt instance class
[vjslib]
java.lang.StringBuffer
[vjslib]
java.lang.StringBuffer::append(string)
IL_0035: callvirt instance string
[vjslib]
java.lang.StringBuffer::ToString()
IL_003a: stloc.0
IL_003b: ldloc.2
IL_003c: ldc.i4.1
IL_003d: add
IL_003e: stloc.2
IL_003f: ldloc.2
IL_0040: ldc.i4 0x3e8
IL_0045: blt.s IL_0020
IL_0047: ldloc.1
IL_0048: callvirt instance void
[System]
System.Diagnostics.Stopwatch::Stop()
IL_004d: ldloc.0
IL_004e: call void
[mscorlib]
System.Console::WriteLine(string)
IL_0053: ldloc.1
IL_0054: callvirt instance int64
[System]
System.Diagnostics.Stopwatch::get_ElapsedMilliseconds()
IL_0059: call void
[mscorlib]
System.Console::WriteLine(int64)
IL_005e: call string
[mscorlib]
System.Console::ReadLine()
IL_0063: pop
IL_0064: call void
[vjslib]
com.ms.vjsharp.util.Utilities::cleanupAfterMainReturns()
IL_0069: br.s IL_006b
IL_006b: ret
} // end of method Program::main
要注意的事项有细微的差别,尤其是在每个代码段开始,在各种编译器所产生的的输出。 VB和C#的CIL看起来非常相似。 我已经把编译器如何宣布大胆的方法开始在所有局部变量 VB控制台向导生成一个模块,而不是类 为什么VB生成一个模块?.method assembly static int32 main(string[] args) cil managed
{
// Code size 83 (0x53)
.maxstack 2
.locals (
[0] class [System]System.Diagnostics.Stopwatch sw,
[1] string strSomeString,
[2] int32 i
)
IL_0000: ldnull
IL_0001: stloc.1
IL_0002: ldnull
IL_0003: stloc.0
IL_0004: ldstr ""
IL_0009: stloc.1
IL_000a: newobj instance void
[System]
System.Diagnostics.Stopwatch::.ctor()
IL_000f: stloc.0
IL_0010: ldloc.0
IL_0011: call instance void
[System]
System.Diagnostics.Stopwatch::Start()
IL_0016: ldc.i4.0
IL_0017: stloc.2
IL_0018: br.s IL_001e
IL_001a: ldloc.2
IL_001b: ldc.i4.1
IL_001c: add
IL_001d: stloc.2
IL_001e: ldloc.2
IL_001f: ldc.i4 0x3e8
IL_0024: bge.s IL_0034
IL_0026: ldloc.1
IL_0027: ldstr "Test "
IL_002c: call string
[mscorlib]
System.String::Concat
(
string,
string
)
IL_0031: stloc.1
IL_0032: br.s IL_001a
IL_0034: ldloc.0
IL_0035: call instance void
[System]
System.Diagnostics.Stopwatch::Stop()
IL_003a: ldloc.1
IL_003b: call void
[mscorlib]
System.Console::WriteLine(string)
IL_0040: ldloc.0
IL_0041: call instance int64
[System]
System.Diagnostics.Stopwatch::get_ElapsedMilliseconds()
IL_0046: call void
[mscorlib]
System.Console::WriteLine(int64)
IL_004b: call string
[mscorlib]
System.Console::ReadLine()
IL_0050: pop
IL_0051: ldc.i4.0
IL_0052: ret
} // end of method 'Global Functions'::main
有VB开发者社区内很多讨论是否模块是面向对象编程(OOP)方法。它是一种常见的误解,是一个随身携带的模块从VB6因此应不惜一切代价避免。虽然对这个热门话题的完整讨论不在本文的讨论范围,我会指出,在VB.NET模块的确是面向对象的。你应该考虑的一个模块,作为一种特殊的类类型,不能用于创建对象。它只能包含共享的成员,它不能包含实例成员。虽然每一个模块的成员是隐式共享,你会遇到一个编译时错误,如果你添加到任何一个Shared关键字。模块没有在其他语言不存在,那么是什么使得VB中如此特别?
NET 2.0的C#语言中的令人振奋的新的增加,除了静态类。静态类和类成员用于创建,而无需创建类的实例可以访问的数据和功能。静态类成员可以使用单独的数据和行为是独立于任何对象标识的数据和功能没有改变,无论对象发生了什么。没有数据或依赖对象的身份在类的行为时,可以使用静态类。因此,静态类可以被认为是C#的回答VB的卑微模块。Whats错误代码?
,虽然这段代码乍一看看起来相当的最佳运作和代码可以编译不抛出各自的编译器,任何讨厌的错误。代码在本质上不是真正的基于。NET代码。如果你要运行的代码的人会发现,简单的字符串连接例程可以采取任何地方在10 - 44 ms(毫秒)来完成,不论你选择什么样的语言来运行。可以这样优化,你可以看到其他什么问题?
{A}我将举例说明怎么连一个像这样的简单的应用程序可以优化。来源
对于一些我的文章的背景资料,我用从Wikipedia上获得的。NET Framework的一些信息。历史06年6月10日初稿第1部分 06年9月10日小幅更新,以提高格式和更改标题06年10月10日更新一些更多的背景资料