UniRx icon indicating copy to clipboard operation
UniRx copied to clipboard

EveryUpdate performance

Open alerad opened this issue 5 years ago • 2 comments

I want to know if having multiple

Observable.EveryUpdate()

on my start function is less performant than having a single one.

Also, It would we good if UniRx had a discord so we as a community can help each other.

thanks!

alerad avatar Oct 25 '19 00:10 alerad

I did some basic stress testing to compare the performance, and the difference is noticeable.

Observable.EveryUpdate() should be used sparingly if CPU and memory performance is of important matter.

List<int> ints = new List<int>(1);
private void Start() {
    ints.AddRange(Enumerable.Range(1, 10000));
    ints.ForEach(x => {
        Observable.EveryUpdate().Subscribe(_ => {
            var a = x;
        });
    });
}

// Update is called once per frame
void Update() {
    ints.ForEach(x => {
        var a = x;
    });
}

The following code was tested, first with the observables commented, then with the Update() commented.

https://i.imgur.com/FOde8jW.png

While the Update loop took only between 1.44ms - 2ms to complete, the UniRX bulk of EveryUpdates took 14-16ms, which shows a significant performance loss.

Although the test is pretty basic, I'd like someones input on my conclusions. Thanks!

alerad avatar Nov 04 '19 21:11 alerad

I think the code is fair.

ints.AddRange(Enumerable.Range(1, 10000)); Observable.EveryUpdate().Subscribe(_ => { ints.ForEach(x => { var a = x; }); });

The main consumption of multiple Observable.EveryUpdate() performance is to instantiate multiple FromMicroCoroutineObservables.

Multiple MonoBehaviour.Update() is mainly consumed by the call of the Unity engine.

I didn't think that a fair way to test the performance of these two methods, but because the Unity engine calls MonoBehaviour.Update() with reflection, I think the UniRx library Observable.EveryUpdate() is faster. I am not sure.

zhuxianzhiniko avatar Nov 05 '19 08:11 zhuxianzhiniko