我正在尝试构建一个代码示例,以便在乘以2的幂时,显示编译器对代码的优化.然而,当我转向优化代码时,IL仍然主要是相同的.我在这里做错了什么想法?
代码:
int nr;
int result;
var stopwatch = new Stopwatch();
nr = 5;
stopwatch.Start();
result = nr * 4;
stopwatch.Stop();
Console.WriteLine(result);
Console.WriteLine(stopwatch.Elapsed.ToString() + "ms ellapsed");
stopwatch.Reset();
stopwatch.Start();
result = nr << 2;
stopwatch.Stop();
Console.WriteLine(result);
Console.WriteLine(stopwatch.Elapsed.ToString() + "ms ellapsed");
Run Code Online (Sandbox Code Playgroud)
非优化IL:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 130 (0x82)
.maxstack 2
.locals init ([0] int32 nr,
[1] int32 result,
[2] class [System]System.Diagnostics.Stopwatch stopwatch,
[3] valuetype [mscorlib]System.TimeSpan CS$0$0000,
[4] valuetype [mscorlib]System.TimeSpan CS$0$0001)
IL_0000: …Run Code Online (Sandbox Code Playgroud) 在我目前的PC设置中,我安装了SSD和普通硬盘.固态硬盘只有120GB的容量,因为我正在开发一个大型的应用程序存储在配发蔚蓝Blob存储我哗哗指定要哪个硬盘驱动器的微软Azure存储模拟器具有存储我的文件的文件.
我正在使用最新的存储模拟器(3.0).
谢谢
今天我正在使用Entity Framework,我已经读过为C#创建的IL与以下代码的VB.NET不同:
VB.NET:
Dim ctx As New TravelEntities
Sub Main()
CallContext()
CallContext()
CallContext()
End Sub
Private Sub CallContext()
Dim someCustomer = From x In ctx.Customer
Where x.CustomerId.Equals(5)
Select x
Console.WriteLine(someCustomer.Count())
End Sub
Run Code Online (Sandbox Code Playgroud)
C#:
private static TravelEntities ctx = new TravelEntities();
static void Main(string[] args)
{
CallContext();
CallContext();
CallContext();
}
private static void CallContext()
{
var someCustomer = from x in ctx.Customer
where x.CustomerId.Equals(5)
select x;
Console.WriteLine(someCustomer.Count());
}
Run Code Online (Sandbox Code Playgroud)
他们产生以下IL:
VB:
.method private static void CallContext() cil managed
{
// Code size …Run Code Online (Sandbox Code Playgroud) 我们构建了一个REST服务,它依赖于accept标头将XML或JSON返回给客户端,或者取决于content-type标头将接受XML或JSON.
现在,最近的集成商问我们是否有针对XML响应和请求的XSD架构.
这是开发REST服务时的常见做法吗?
今天我在VB.NET中目睹了一些非常奇怪的行为.我正在谈论的代码如下:
Option Strict On
Option Explicit On
Module Module1
Sub Main()
Dim thisShouldBeSet = False
DoSomething(Function() thisShouldBeSet = True)
If Not thisShouldBeSet Then
Throw New Exception()
End If
Console.WriteLine("yaay")
End Sub
Sub DoSomething(action As Action)
action.Invoke()
End Sub
End Module
Run Code Online (Sandbox Code Playgroud)
我知道代码本身存在缺陷,因为我必须使用:
DoSomething(Sub() thisShouldBeSet = True)
Run Code Online (Sandbox Code Playgroud)
代替:
DoSomething(Function() thisShouldBeSet = True)
Run Code Online (Sandbox Code Playgroud)
但我觉得很奇怪,即使使用Option Strict和Option Explicit,编译也允许我编译这段代码.
更奇怪的是,在运行代码时,Action实际上表现得像一个Func(布尔值).
任何人都可以向我提供有效解释为什么在VB.NET中允许这样做?这是编译器/运行时错误吗?