English 中文(简体)
如何在 Java扩大阿雷拉的多数有效途径?
原标题:Most Efficient Way to Scale an Array in Java?

(如果事先提出这种意见的话,我可以不相信这种说法,但我找不到。) 也许我的搜索胜地薄弱。

几年来,“已知” Java没有本地功能扩大阵列(即每件元素以恒定数乘)。 因此,我一直在这样做:

for (int i=0; i<array.length; i++) {
  array[i] = array[i] * scaleFactor;
}

这实际上是否是最有效的方式(例如,在这项申请中,它有大约10 000倍的双倍)? 还是有更好的办法?

最佳回答

对我来说,这是绝对的。 我无法想出更有效的方法。 显然,试图将这一守则放在一个地方,而不是把实际守则放在一个地方,但除此之外,没有明显的问题。

问题回答

我所能提出的唯一其他建议是谨慎地缩小规模,只有这样,你才能在获取每个要素方面支付倍增的费用;例如,

public class MyArray {
  private final double[] arr;
  private double scale = 1.0;

  public MyArray(double[] arr) {
    this.arr = arr;
  }

  public double getScale() {
    return scale;
  }

  public void setScale(double scale) {
    this.scale = scale;
  }

  public double elementAt(int i) {
    return arr[i] * scale;
  }
}

显然,在某些情况下,这只会更好:

  • When your array is huge AND
  • You are only accessing a few elements AND
  • You are typically accessing these elements once.

在其他情况下,它促进微粒生产,而现代集体企业则没有真正受益。

“更好的方式”是: 书写<条码>, * = 尺度;,而不是<条码>.array[i] = 阵列* 比例;:-

Really, that s just syntactic sugar though - the compiled output (and hence performance) should be exactly the same. As Jon says, you re not going to be able to get any better performance, but personally I ll take a reduction in typing any day.

除了Adamski和Joon Skeet之外,我只能想补充的是,如果是一系列的内ts/长s,而且你将二分之一的电击率重新升降,那么,通过使用临时操作员,你可能会稍有改善。 但是,由于它将取决于汇编者(甚至可能还有证书)。

In Java 8:

double coef = 3.0;
double[] x1 = {1,2,3};
double[] x2 = DoubleStream.of(x1).map(d->d*coef).toArray();

System.out.println(Arrays.toString(x2));

output: [3.0, 6.0, 9.0]

You could work with threads, to reduce the runtime, but the bottom line is you would include this code and let each thread run a part of the for loop so the resulting program is as efficient as yours; it s just made faster

对我来说是最好的。

Don false false opt opt 这种做法通过避免重复使用方法进行收集,使(......)和“Stings”避免使用这种方法。

此外,向零向后退可能是一种选择使用组装语言,但如Java一样,考试和测验科将注意任何明显的ak。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签