Kotlin-使用Array< Double>或DoubleArray [英] Kotlin - use Array<Double> or DoubleArray

查看:185
本文介绍了Kotlin-使用Array< Double>或DoubleArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两者的主要区别是什么?

What is the main difference in these two:

val array: Array<Double> = arrayOf()

vs

val array: DoubleArray = doubleArrayOf()

我知道一个正在使用原始数据类型double,第二个正在使用其基于对象的countrepart Double.

I know that one is using primitive data type double and the second its object based countrepart Double.

使用普通DoubleArray是否有任何惩罚或不满意?

Is there any penalty or disadvatnage in using plain DoubleArray?

为什么我想知道:

我正在使用JNI,对于Double,我必须致电

I am using JNI and for Double, I have to call

 jclass doubleClass = env->FindClass("java/lang/Double");
 jmethodID doubleCtor = env->GetMethodID(doubleClass, "<init>", "(D)V");
 jobjectArray res = env->NewObjectArray(elementCount, doubleClass, nullptr);

 for (int i = 0; i < elementCount; i++){
    jobject javaDouble = env->NewObject(doubleClass, doubleCtor, array[i]);
    env->SetObjectArrayElement(res, i, javaDouble);
    env->DeleteLocalRef(javaDouble);
 }

vs

jdoubleArray res = env->NewDoubleArray(elementCount);
env->SetDoubleArrayRegion(res, 0, elementCount, array);

推荐答案

没有任何惩罚(实际上,由于没有装箱,它会更快),但是,与Java中的原始类型一样,它迫使您创建专门的如果您希望能够与[Int/Double/etc]Array一起使用某些方法,请重载.

There is no penalty (in fact, it will be faster due to no boxing), but, as with primitive types in Java, it forces you to create specialized overloads of certain methods if you want to be able to use them with [Int/Double/etc]Array.

此实际上已经在Kotlin论坛上进行了讨论:

整数数组的内存布局与对象指针数组的内存布局完全不同.

the memory layout of an array of integers is quite different from that of an array of object pointers.

在讨论中Norswap的评论很好地总结了权衡:

Norswap's comment in that discussion summarizes the tradeoff quite well:

原生的[int[]/IntArray]读/写速度更快,但是包装的[Integer[]/Array<Int>]不需要在每次跨越通用边界时都进行完全转换. br> #7,norswap

The native one [int[]/IntArray] is faster to read/write, but the wrapped [Integer[]/Array<Int>] does not need to be entirely converted each time it crosses a generic boundary.
#7, norswap

例如,接受Array<Int>(JVM上为Integer[])的函数将接受IntArray(int[]).

For example, a function accepting Array<Int> (Integer[] on the JVM) will not accept an IntArray (int[]).

您已经列出了唯一的实际区别,其中一个被编译为原语double[],另一个被编译为Double[].但是,Double[] objects 的数组,因此,每当您通过将值设置为double或检索double修改数组时,将分别执行装箱和拆箱

You have already listed the only real difference, that one is compiled to the primitive double[] and the other to Double[]. However, Double[] is an array of objects, so any time you modify the array by setting a value to a double, or retrieve a double, boxing and unboxing will be performed, respectively.

出于速度和内存原因,通常建议改用DoubleArray.

It is usually recommended to use DoubleArray instead, for speed and memory reasons.

以对象包装器导致的速度损失为例,请查看帖子的开头来自有效Java:

As an example of speed penalties due to the object wrappers, take a look at the start of this post, taken from Effective Java:

public static void main(String[] args) {
    Long sum = 0L; // uses Long, not long
    for (long i = 0; i <= Integer.MAX_VALUE; i++) {
        sum += i;
    }
    System.out.println(sum);
}

long替换Long可使运行时间从43秒降低到8秒.

Replacing Long with long brings runtime from 43 seconds down to 8 seconds.

这篇关于Kotlin-使用Array&lt; Double&gt;或DoubleArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆