创建计数数组的最有效方法

时间:2012-06-08 23:28:13

标签: java arrays performance

制作给定长度数组的最有效方法是什么,每个元素都包含其下标?

我的虚拟级代码可能的描述:

/**
 * The IndGen function returns an integer array with the specified dimensions.
 *  
 *  Each element of the returned integer array is set to the value of its 
 *  one-dimensional subscript.
 *  
 *  @see Modeled on IDL's INDGEN function: 
 *      http://idlastro.gsfc.nasa.gov/idl_html_help/INDGEN.html 
 *  
 *  @params size
 *  @return int[size], each element set to value of its subscript
 *  @author you
 *  
 *  */

public int[] IndGen(int size) {
    int[] result = new int[size];
    for (int i = 0; i < size; i++) result[i] = i;
    return result;
}

其他提示,例如doc style,welcome。

修改

我在其他地方读过for循环与其他方法相比效率低的问题,例如在Copying an Array中:

  

使用克隆:93毫秒

     

使用System.arraycopy:110毫秒

     

使用Arrays.copyOf:187 ms

     

使用for循环:422 ms

我对本网站上一些问题的富有想象力的回答给我留下了深刻的印象,例如Display numbers from 1 to 100 without loops or conditions。这是一个可能提出一些方法的答案:

public class To100 {
public static void main(String[] args) {
        String set = new java.util.BitSet() {{ set(1, 100+1); }}.toString();
        System.out.append(set, 1, set.length()-1);
    }
}

如果你不能解决这个具有挑战性的问题,无需发泄:只需转到下一个未解决的问题,你可以处理的问题。

3 个答案:

答案 0 :(得分:3)

由于一次使用太字节存储器是不可行的,尤其是同时使用它们进行任何计算,您可能会考虑使用发生器。 (您可能正计划在数组上循环,对吧?)使用生成器,您不需要初始化数组(因此您可以立即开始使用它)并且几乎不使用任何内存(O(1))。 / p>

我在下面列出了一个示例实现。它受long原语的限制所限制。

import java.util.Iterator;
import java.util.NoSuchElementException;

public class Counter implements Iterator<Long> {
    private long count;
    private final long max;

    public Counter(long start, long endInclusive) {
        this.count = start;
        this.max = endInclusive;
    }

    @Override
    public boolean hasNext() {
        return count <= max;
    }

    @Override
    public Long next() {
        if (this.hasNext())
            return count++;
        else
            throw new NoSuchElementException();

    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException();
    }
}

在下面找到一个用法演示。

Iterator<Long> i = new Counter(0, 50);
while (i.hasNext()) {
    System.out.println(i.next());  // Prints 0 to 50
}

答案 1 :(得分:2)

我唯一想到的是使用“++ i”而不是“i ++”,但我认为java编译器已经进行了这种优化。

除此之外,这几乎是最好的算法。

你可以创建一个类似于它有一个数组但却没有它的类,并且它只返回它获得的相同数字(也就是身份函数),但这不是你要求的

答案 2 :(得分:0)

正如其他人在他们的回答中所说,你的代码已经接近我能想到的最有效率,至少对于小型数组而言。如果你需要很多次创建这些数组并且它们非常大,而不是在for循环中连续迭代,你可以创建一次所有数组,然后复制它们。如果阵列非常大,复制操作将比遍历数组更快。它将是这样的(在这个例子中最多1000个元素):

public static int[][] cache = {{0},{0,1},{0,1,2},{0,1,2,3},{0,1,2,3,4}, ..., {0,1,2,...,998,999}};

然后,从您需要多次创建这些数组的代码中,您将使用以下内容:

int[] arrayOf50Elements = Arrays.copyOf(cache[49], 50);

请注意,这种方式使用大量内存来提高速度。我想强调的是,当你需要多次创建这些数组时,这只会是复杂的,数组非常大,最高速度是你的要求之一。在我能想到的大多数情况下,你提出的解决方案将是最好的解决方案。

编辑:我刚刚看到了您需要的大量数据和内存。我建议的方法需要n ^ 2的内存,其中n是你期望的最大整数。在这种情况下,由于你需要大量的内存,这是不切实际的。算了吧。我离开这个帖子是因为它可能对其他人有用。

相关问题