我试图从某些内部方法中获得尽可能多的性能。
Java代码是:
List<DirectoryTaxonomyWriter> writers = Lists.newArrayList();
private final int taxos = 4;
[...]
@Override
public int getParent(final int globalOrdinal) throws IOException {
final int bin = globalOrdinal % this.taxos;
final int ordinalInBin = globalOrdinal / this.taxos;
return this.writers.get(bin).getParent(ordinalInBin) * this.taxos + bin; //global parent
}
Run Code Online (Sandbox Code Playgroud)
在我的分析器中,我看到 1% 的 CPU 支出java.util.Objects.requireNonNull
,但我什至不称之为。在检查字节码时,我看到了:
public getParent(I)I throws java/io/IOException
L0
LINENUMBER 70 L0
ILOAD 1
ALOAD 0
INVOKESTATIC java/util/Objects.requireNonNull (Ljava/lang/Object;)Ljava/lang/Object;
POP
BIPUSH 8
IREM
ISTORE 2
Run Code Online (Sandbox Code Playgroud)
所以编译器会生成这个(没用的?)检查。我在原语上工作,null
无论如何都不能,那么编译器为什么会生成这一行?这是一个错误吗?还是“正常”行为?
(我可能会使用位掩码,但我只是好奇)
[更新]
运营商似乎与它无关(请参阅下面的答案)
使用 …
在 C# 中,我可以编写代码来检查空引用,以防抛出自定义异常,例如:
var myValue = (someObject?.SomeProperty ?? throw new Exception("...")).SomeProperty;
Run Code Online (Sandbox Code Playgroud)
在最近的更新中,TypeScript 引入了空合并运算符 ?? 但是像上面的语句一样使用它会产生编译错误。TypeScript 中是否有一些类似的允许语法?
澄清一下,所需的行为是通过以下代码实现的:
if(someObject?.someProperty == null) {
throw new Error("...");
}
var myValue = someObject.someProperty.someProperty;
Run Code Online (Sandbox Code Playgroud)
编码:
var myValue = someObject?.someProperty.someProperty;
Run Code Online (Sandbox Code Playgroud)
在逻辑上正常工作,但抛出一个意义不大的异常。
我发现自己检查了这一点,并询问是否有必要.我有这样的代码:
public Object myMethod(Object... many) {
if (many == null || many.length == 0)
return this;
for (Object one : many)
doSomethingWith(one);
return that;
}
Run Code Online (Sandbox Code Playgroud)
但后来我想知道......我是否过于谨慎?我需要检查一下many == null
吗?在任何当前的Java版本中都有可能吗?如果是这样,怎么样?如果没有,我可能会继续检查,只是为了防止以后Oracle确定它有一天可以为空.
我有一段代码,其中一个接口有一个Optional返回方法,一些类实现它返回一些东西,其他没有.
为了拥抱这个辉煌的"空杀手",这是我尝试过的:
public interface Gun {
public Optional<Bullet> shoot();
}
public class Pistol implements Gun{
@Override
public Optional<Bullet> shoot(){
return Optional.of(this.magazine.remove(0));
}//never mind the check of magazine content
}
public class Bow implements Gun{
@Override
public Optional<Bullet> shoot(){
quill--;
return Optional.empty();
}
}
public class BallisticGelPuddy{
private Gun[] guns = new Gun[]{new Pistol(),new Bow()};
private List<Bullet> bullets = new ArrayList<>();
public void collectBullets(){
//here is the problem
for(Gun gun : guns)
gun.shoot.ifPresent(bullets.add( <the return I got with the method>)
}} …
Run Code Online (Sandbox Code Playgroud) 我有一个带有两个覆盖==运算符的类,将它与该类的其他实例进行比较,并与string的实例进行比较.
class SomeClass
{
string value;
public SomeClass (string _Value)
{
value = _Value;
}
static public bool operator == (SomeClass C1, SomeClass C2)
{
return C1.value == C2.value;
}
static public bool operator != (SomeClass C1, SomeClass C2)
{
return C1.value != C2.value;
}
static public bool operator == (SomeClass C1, string C2)
{
return C1.value == (string) C2;
}
static public bool operator != (SomeClass C1, string C2)
{
return C1.value != (string) C2;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试将此类与null进行比较时:
Console.WriteLine(someObject …
Run Code Online (Sandbox Code Playgroud) 所以,我在应用程序中做了很多数据库工作 - 我的缓存系统有几个可能的返回值.它可以返回null,它可以返回默认值(类型),也可以返回无效对象(无效对象,我的意思是属性/值不正确).我想创建一个扩展方法来为我做所有这些检查,如下所示:
public static bool Valid<T> (this T obj) where T: class
{
if (obj == null)
return false;
else if (obj == default(T))
return false;
//Other class checks here
else
return true;
}
Run Code Online (Sandbox Code Playgroud)
问题是,我的编译器告诉我if (obj == default(T))
将永远是假的.
这是为什么?
我需要在这个公式[i]中创建一个空检查,我不完全确定如何解决这个问题,因为我不太熟悉null检查和编程方面的新功能.任何和所有的帮助非常感谢!
public static double calculateInventoryTotal(Book[] books)
{
double total = 0;
for (int i = 0; i < books.length; i++)
{
total += books[i].getPrice();
}
return total;
}
Run Code Online (Sandbox Code Playgroud) 我们有Null合并操作符.NET
,我们可以使用如下
string postal_code = address?.postal_code;
Run Code Online (Sandbox Code Playgroud)
我们可以在React JS中做同样的事情吗?
我发现我们可以使用&&运算符
在address.ts文件中
string postal_code = address && address.postal_code;
Run Code Online (Sandbox Code Playgroud)
我需要什么样的.net功能可以在typescript中使用react JS,这可能吗?
就像是:
string postal_code = address?.postal_code // I am getting the error in this line if I try to use like .NET
Run Code Online (Sandbox Code Playgroud) 据我了解,?.let{}
over的最大优点!= null
是它保证了块内的可变值不会改变。
但是,在不可变变量的情况下是否存在性能差异?
例如,我有一个简单的方法
private fun test() {
val x: String? = ""
if (x != null) {
print("test")
}
x?.let {
print("test")
}
}
Run Code Online (Sandbox Code Playgroud)
当我看到生成的 Kotlin 字节码时,它似乎有更多的代码。
对于 x != null 情况:
LINENUMBER 8 L1
L2
LINENUMBER 9 L2
LDC "test"
ASTORE 2
L3
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
ALOAD 2
INVOKEVIRTUAL java/io/PrintStream.print (Ljava/lang/Object;)V
L4
L5
Run Code Online (Sandbox Code Playgroud)
对于 x?.let { } 情况是:
LINENUMBER 12 L5
ALOAD 1
ASTORE 2
L6
L7
ALOAD 2
ASTORE 3
L8
ICONST_0 …
Run Code Online (Sandbox Code Playgroud) 我正在使用旧版应用程序,在该应用程序中,我经常不得不访问像下面这样嵌套的属性:
a.getB().getC().getD().getE().getF()
Run Code Online (Sandbox Code Playgroud)
问题在于,在任何深度下,该值都可能为空。当然,我可以像这样检查每个深度:
public Optional<F> retrieveValue(A a) {
if(a != null && a.getB() != null && a.getB().getC() != null &&
a.getB().getC().getD() != null &&
a.getB().getC().getD().getE() != null &&
a.getB().getC().getD().getE().getF() != null) {
return Optional.of(a.getB().getC().getD().getE().getF());
} else {
return Optional.empty();
}
}
F existingOrCreated = retrieveValue(a).orElse(new F());
Run Code Online (Sandbox Code Playgroud)
但是我必须在许多地方对许多不同的对象执行此操作,因此这些检查会使代码膨胀。大多数情况下,对象不是非null,但是在少数情况下,对象为null。有没有一种更简洁的方法?我正在使用Java 8,但是无法更新提供的旧代码。
null-check ×10
java ×5
c# ×2
java-8 ×2
typescript ×2
.net ×1
equality ×1
javascript ×1
kotlin ×1
linq ×1
modulo ×1
null ×1
optional ×1
performance ×1
reactjs ×1
resharper ×1
syntax ×1