问题
I have an array, for example (in Java)
int[] a = new int[N];
I have worked with it and now want to have array with zeros. What will take less time: to create a new Array(it will be initialized will zeros) or iterate through existing one and fill it with zeros?
I suppose whatever answer is, it will be the same in C++ ?
回答1:
Chances are that you're better off filling an existing array than creating a new one. Memory allocation can be very expensive relatively speaking. Indeed, if your favorite language provides you new arrays that are guaranteed to be zeroed out, it is probably filling them for you under the covers.
That said, this type of choice is a micro-optimization. In most cases, it won't make any discernible difference. And if you find yourself in a specific case where you think it might make an important difference, you're much better asking a profiler than asking StackOverflow.
Edit I'll add one more caveat: Particularly in garbage collected languages like Java, you're better off reusing existing objects than creating new ones if the reuse can be done cleanly. As a general rule of thumb.
Reedit ... Unless the object(s) in question are expected to be very short lived. Probably some additional caveats too. And so back to "ask the profiler."
回答2:
When creating a new Array and instantiating it with zeros, it should be slower. In this case the initialization and the iteration is done. In the case of using an existing array, only the iteration bit is done. Therefore using an existing array and iterate over it should be faster. Of course, we talk ms here, the initialization of a new Array doesn't take long in general.
回答3:
This is a Java centric answer, as I have no C++ experience.
It can vary, depending on which method you choose. According to Joshua Bloch's Effective Java:
It is better to "... prefer primitives to boxed primitives and watch out for unintentional autoboxing".
What this means is an array allocated as
int[] array = new int[1000];
will have a much smaller memory footprint and be faster to create than
Integer[] array = new Integer[1000];
due to not requiring the autoboxing of int
values inside Integer
objects.
Also, remember that the creation and reclamation of small objects whose constructors do little explicit work is cheap, especially on modern JVM implementations.
In short, it really doesn't matter. If you do benchmark the tests, I'd be surprised if the difference is more than a few microseconds.
回答4:
I have tested it.
public class Test{
public static void main(String[] args)
{
int[] b = new int[1000000];
for(int i = 0; i < b.length; i++)
b[i] = i;
long t1 = System.currentTimeMillis();
int[] a = new int[1000000];
long t2 = System.currentTimeMillis();
System.out.println("Time to alloc: " + (double) (t2 - t1) / 1000);
long t3 = System.currentTimeMillis();
for(int i = 0; i < b.length; i++)
b[i] = 0;
long t4 = System.currentTimeMillis();
System.out.println("Time to iterate: " + (double) (t4 - t3) / 1000);
}
}
/*output
Time to alloc: 0.004
Time to iterate: 0.001
*/
So we have iteration about 4 times faster then allocation new.
来源:https://stackoverflow.com/questions/11129911/what-is-the-faster-to-create-a-new-array-or-iterate-through-existing