Tag: il

为什么编译器让我在C#中将特定的types转换为null?

考虑这个代码: var str = (string)null; 当写代码这是我的IL代码: IL_0001: ldnull IL有Cast操作符,但是: var test = (string) new Object(); IL代码是: IL_0008: castclass [mscorlib]System.String 所以将null强制转换为string被忽略。 为什么编译器让我把null成特定的types?

C#“是”运算符在.NET 4上的释放模式优化下受到影响吗?

下面是一个简单的testing夹具。 它在debugging版本中成功并在发布版本(VS2010,.NET4解决scheme,x64)中失败: [TestFixture] public sealed class Test { [Test] public void TestChecker() { var checker = new Checker(); Assert.That(checker.IsDateTime(DateTime.Now), Is.True); } } public class Checker { public bool IsDateTime(object o) { return o is DateTime; } } 看起来代码优化造成了一些破坏。 如果我在Release版本上禁用它,它也可以工作。 这令我感到困惑。 下面我使用ILDASM来反汇编这个版本的两个版本: debuggingIL: .method public hidebysig instance bool IsDateTime(object o) cil managed { // Code size […]

stackoverflow做拳击在C#

我在C#中有这两个代码块: 第一 class Program { static Stack<int> S = new Stack<int>(); static int Foo(int n) { if (n == 0) return 0; S.Push(0); S.Push(1); … S.Push(999); return Foo( n-1 ); } } 第二 class Program { static Stack S = new Stack(); static int Foo(int n) { if (n == 0) return 0; S.Push(0); S.Push(1); […]

静态方法的性能与实例方法

我的问题是关于静态方法的性能特征与实例方法及其可伸缩性。 假设这种情况下所有的类定义都在一个程序集中,并且需要多个离散的指针types。 考虑: public sealed class InstanceClass { public int DoOperation1(string input) { // Some operation. } public int DoOperation2(string input) { // Some operation. } // … more instance methods. } public static class StaticClass { public static int DoOperation1(string input) { // Some operation. } public static int DoOperation2(string input) { // Some […]

为什么我的应用程序花费24%的生命做空检查?

我有一个性能重要的二叉决策树,我想把这个问题集中在一行代码上。 二叉树迭代器的代码如下,运行性能分析的结果。 public ScTreeNode GetNodeForState(int rootIndex, float[] inputs) { 0.2% ScTreeNode node = RootNodes[rootIndex].TreeNode; 24.6% while (node.BranchData != null) { 0.2% BranchNodeData b = node.BranchData; 0.5% node = b.Child2; 12.8% if (inputs[b.SplitInputIndex] <= b.SplitValue) 0.8% node = b.Child1; } 0.4% return node; } BranchData是一个字段,而不是一个属性。 我这样做是为了防止不被内联的风险。 BranchNodeData类如下所示: public sealed class BranchNodeData { /// <summary> /// The […]

为什么C#编译器把这个!=比较翻译成比较呢?

我有纯粹的机会发现,C#编译器转变此方法: static bool IsNotNull(object obj) { return obj != null; } …进入这个CIL : .method private hidebysig static bool IsNotNull(object obj) cil managed { ldarg.0 // obj ldnull cgt.un ret } …或者,如果你喜欢看反编译的C#代码: static bool IsNotNull(object obj) { return obj > null; // (note: this is not a valid C# expression) } 为什么!=被翻译成“ > ”?