- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
以下是否有任何性能问题(例如执行装箱)?
public int CompareIntValues(int left, int right)
{
return left.CompareTo(right);
}
一些进一步的信息。该应用程序应该是软实时的,因此使用 C# 可能是一个奇怪的选择。但是,这不在我的控制范围内。
最佳答案
是时候播放大家最喜欢的游戏节目了:BOX OR NO BOX!
public string DoIntToString(int anInt)
{
return anInt.ToString();
}
BOX 还是NO BOX?让我们转到 IL:
IL_0001: ldarga.s anInt
IL_0003: call instance string [mscorlib]System.Int32::ToString()
没有盒子。 ToString()
是 virtual
object
上的方法被 int
覆盖.自 struct
s不能参与非接口(interface)继承,编译器知道没有int
的子类, 并且可以生成对 int
的调用ToString()
的版本直接。
static Type DoIntGetType(int anInt)
{
return anInt.GetType();
}
BOX 还是NO BOX?让我们转到 IL:
IL_0001: ldarg.0
IL_0002: box [mscorlib]System.Int32
IL_0007: call instance class [mscorlib]System.Type [mscorlib]System.Object::GetType()
盒子。 GetType()
不是 virtual
在 object
, 所以没有 int
方法的版本。必须对参数进行装箱,并在新的装箱对象上进行调用。
private static string DoIntToStringIFormattable(int anInt)
{
return anInt.ToString(CultureInfo.CurrentCulture);
}
BOX 还是NO BOX?让我们转到 IL:
IL_0001: ldarga.s anInt
IL_0003: call class [mscorlib]System.Globalization.CultureInfo [mscorlib]System.Globalization.CultureInfo::get_CurrentCulture()
IL_0008: call instance string [mscorlib]System.Int32::ToString(class [mscorlib]System.IFormatProvider)
没有盒子。尽管ToString(IFormattable)
是 IFormatProvider
的一个实现接口(interface),调用本身是直接针对 int
进行的.同第一种方法一样,不需要盒子。
所以对于最后一轮,我们有你的方法:
public int CompareIntValues(int left, int right)
{
return left.CompareTo(right);
}
知道CompareTo(int)
是 IComparable<int>
的隐式实现,你做出决定:BOX 还是 NO BOX?
关于c# - Int32.CompareTo(int x) 性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7162322/
这似乎很奇怪,这并没有像我预期的那样工作。我编写了一个简单的java类,它实现Comparable接口(interface)并重写compareTo()方法。但是,它不允许我传递除对象之外的特定类型的
public int compareTo(Object another) throws CustomMadeException { if(this.getClass() != another.
早些时候,我在通用 IComparable 中获得了逆变的具体示例Jon Skeet 的界面。然而,这又产生了另一个问题。为什么不是通用的 List.Sort()方法能够推断出相同的信息? 我在这里提
我有注释 package javaannotationtest; import java.lang.annotation.*; @Target({ElementType.METHOD}) @Reten
我试图理解 C# 中的 CompareTo(),下面的示例让我比以往任何时候都更加困惑。有人能帮我理解为什么第三个变体的结果是 1 吗?句子“Hello wordd”中的第二个词与 str1“Hell
我已经在这个程序上工作了几天,并且在我的 BinarySearchTree 类中实现了一些主要方法,例如插入和删除。插入似乎工作正常,但是一旦我尝试删除,我就会不断出错。因此,在玩弄了代码之后,我想测
首先,我必须提到的是,在阅读了大量问题和教程并观看了一些视频后,问题仍然没有解决。 我是一名Java中级程序员,我编写了一些用于比较优先级队列中的元素的代码,其中元素的保存方式类似于[Comparab
我只是在为即将到来的考试做一些复习。我发现了我们的讲师给我们提供的这段代码供我们修改。 代码: public class Employee implements Comparable{ private
我的书要求我为一段代码编写 Javadoc 注释。大多数情况下,我了解如何执行 javadocs,但我不了解该程序在做什么。 “为类 Person 的以下方法编写 Javadoc 注释。假设类 Per
如果我写以下内容是否有可能溢出: public class SomeObj implements Comparable { private final float data; pub
我有一个名为任务的类,我想将其放入 PriorityQueue 中。 我的类(class)通过日期和名为isUrgent的 boolean 字段进行比较 @Override publ
这个问题已经有答案了: Java error: Comparison method violates its general contract (13 个回答) 已关闭 7 年前。 我有这个compa
我的类(class)结构: public class Priorityy implement Comparable { public int compareTo(Object pe) {
给定一个非负整数列表,我想对它们进行排列,使它们形成最大的数字。给定 [1, 20, 23, 4, 8],最大的形成数字是 8423201。但我想弄清楚compareTo 方法中变量的顺序如何影响 A
我想通过上次联系日期比较两个“收件人”,如果相同,则通过地址进行比较。这是我的代码: public class RecipientComparator implements Comparator {
package Comparable; public class Movie implements Comparable { private double rating; privat
您好,我在实现compareTo 方法时遇到问题。我一直在寻找答案,但没有任何帮助。我正在尝试用各种大小的圆圈填充 TreeSet。我需要在我的圈子类中使用compareTo 才能以这种方式存储它们。
我有以下代码;目的是返回数组中按字母顺序排列的最小成员。 public String smallest() { String smallest = ""; int i = 0; while(log[i
我有一个关于compareTo函数如何协助比较器进行排序的问题即 o1.compareTo(o2) 与 o2.compareTo(o1) 如果两个字符串相等,则此方法返回 0,否则返回正值或负值。如果
帮助我无法弄清楚compareTo函数。这就是我必须做的:编写一个compareTo函数,可用于根据以下内容按顺序放置产品到他们的零件号。也就是说,后面按字母顺序排列的零件号顺序大于按字母顺序排列较早
我是一名优秀的程序员,十分优秀!