Unity-Technologies / com.unity.webrtc

WebRTC package for Unity
Other
738 stars 185 forks source link

Audio samples mute optimization #969

Closed MaximKurbanov closed 9 months ago

MaximKurbanov commented 10 months ago

Array.Clear takes 3/5 the time than array[i] = 0

 private static void Test1()
    {
        InitData(out var data);

        var sw = Stopwatch.StartNew();
        for (int i = 0; i < data.Length; i++)
        {
            data[i] = 0;
        }
        sw.Stop();

        Debug.Log($"test1: {sw.ElapsedMilliseconds}"); //About 50
    }

    private static void Test2()
    {
        InitData(out var data);

        var sw = Stopwatch.StartNew();
        Array.Clear(data, 0, data.Length);
        sw.Stop();

        Debug.Log($"test2: {sw.ElapsedMilliseconds}");//About 30
    }

    private static void InitData(out float[] data)
    {
        data = new float[100_000_000];
        UnityEngine.Random.InitState(1);
        for (int i = 0; i < data.Length; i++)
        {
            data[i] = UnityEngine.Random.Range(0.0f, 1.0f);
        }
    }
unity-cla-assistant commented 10 months ago

CLA assistant check
All committers have signed the CLA.

karasusan commented 9 months ago

@MaximKurbanov

Hi, Thanks for your contribution! But I got the error below.

Packages\com.unity.webrtc\Runtime\Scripts\AudioCustomFilter.cs(44,17): error CS0103: The name 'Array' does not exist in the current context