Copy Array

Is there a faster way to make a complete shallow copy of a builtin array?

The following function can copy an array with 1600 Vector4s from ‘a’ to ‘b’ 100times per update at 0.5-0.6 FPS

	/* Copy Data from b to a */
	function CopyArrayDataA (a, b)
	{
		var tempArray : Vector4[] ;
			tempArray = new Vector4[(simWidth  * simHeight)] ; 
		for (i = 0; (i) < (simWidth  * simHeight);  i++)
		{
			tempArray[i] = b[i];
		}
		return (tempArray);
    }

and this function can copy an array with 1600 Vector4s from ‘a’ to ‘b’ 100times per update at 2.2-2.5 FPS:

	/* Copy Data from b to a */
	function CopyArrayData2 (a, b)
	{
		var newArray  = new Array (b);
		var newBarray : Vector4[] = newArray.ToBuiltin(Vector4);
		return (newBarray);
    }

Cheers,
T

I’m guessing this is in Javascript? I don’t know if you’d have better performance in C# or not.

I can’t speak to the speed of using “ToBuiltin”. However, in your first example, one way of speeding it up would be to not keep recalculating the size:

var size = simWidth * simHeight;
for (i = 0; i < size; i++)

I think if you also statically type your variables, including “i” in the for loop it should compile those in. But I’m not too sure; I don’t code Unity in languages other than C#.

I would expect

Syste.Array.Copy(arr1, arr2, length)

to be faster

Actually, this code

Vector3[] tempArray = (Vector3[]) originalArray.Clone();

give me the same result, but I’m wondering if System.Array.Copy is faster like ivkoni said.

I would guess so, because of the cast from an Object type to a Vector3 array.

EDIT: After some benchmark on a 27 000 size array, it seems to be faster with the Clone function. Unless the process of copying the array needs to reallocate memory, it takes soo much time. (I’m testing on IOS device)

Another point, if you allocate the size for your array in advance, it’s gonna be much faster as well!

Strange, I am doing some sound synth with
OnAudioFilterRead and Array.Copy was a lot faster then array.Clone()

1 Like

You may have missed that this thread is ten years old. A lot has changed since. Especially since Unity does not make any promises for .net system library performance, quote:

Unity makes no performance nor allocation guarantees of the .NET system libraries across Unity versions. As a general rule of thumb, Unity does not fix any performance regressions in the .NET system libraries.

Source: Unity - Manual: Overview of .NET in Unity

If I interpret that correctly then different Unity versions may have different performance results for system libs and regressions (for example if things get slower) are not considered a bug.

Was it really, really fast!

I’m looking to make a buffer outside of OnAudioFilterRead, and then copy in segments of it to satisfy the needs of its data, each time it gets a call.

You think this is the fastest way to do it?

1 Like

I’ve used an external DLL with also functions like multiplyarrayby, or addarray and of course copy array, but maybe C# ILPCC is quicker and better solution these days. As interop to external dll’s will probably also cause latency compared to native c#