Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Copy Array

Discussion in 'Scripting' started by tharyn, Jun 29, 2010.

  1. tharyn

    tharyn

    Joined:
    Oct 3, 2009
    Posts:
    36
    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
    Code (csharp):
    1.     /* Copy Data from b to a */
    2.     function CopyArrayDataA (a, b)
    3.     {
    4.         var tempArray : Vector4[] ;
    5.             tempArray = new Vector4[(simWidth  * simHeight)] ;
    6.         for (i = 0; (i) < (simWidth  * simHeight);  i++)
    7.         {
    8.             tempArray[i] = b[i];
    9.         }
    10.         return (tempArray);
    11.     }
    and this function can copy an array with 1600 Vector4s from 'a' to 'b' 100times per update at 2.2-2.5 FPS:
    Code (csharp):
    1.     /* Copy Data from b to a */
    2.     function CopyArrayData2 (a, b)
    3.     {
    4.         var newArray  = new Array (b);
    5.         var newBarray : Vector4[] = newArray.ToBuiltin(Vector4);
    6.         return (newBarray);
    7.     }

    Cheers,
    T
     
  2. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    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:
    Code (csharp):
    1. var size = simWidth * simHeight;
    2. 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#.
     
  3. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    I would expect
    Code (csharp):
    1. Syste.Array.Copy(arr1, arr2, length)
    to be faster
     
  4. Matt_001

    Matt_001

    Joined:
    Sep 17, 2010
    Posts:
    101
    Actually, this code

    Code (csharp):
    1.  
    2. Vector3[] tempArray = (Vector3[]) originalArray.Clone();
    3.  
    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)
     
    Last edited: Mar 4, 2011
  5. Matt_001

    Matt_001

    Joined:
    Sep 17, 2010
    Posts:
    101
    Another point, if you allocate the size for your array in advance, it's gonna be much faster as well!
     
  6. badjano

    badjano

    Joined:
    Aug 2, 2012
    Posts:
    20
    Strange, I am doing some sound synth with
    OnAudioFilterRead and Array.Copy was a lot faster then array.Clone()
     
    Marald likes this.
  7. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    667
    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: https://docs.unity3d.com/Manual/overview-of-dot-net-in-unity.html

    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.
     
  8. april_4_short

    april_4_short

    Joined:
    Jul 19, 2021
    Posts:
    489
    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?
     
    Marald likes this.
  9. Marald

    Marald

    Joined:
    Jan 16, 2015
    Posts:
    40
    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#