Created
January 25, 2018 21:43
-
-
Save Protonz/05f5370ec36494da74890b150490559a to your computer and use it in GitHub Desktop.
Experimenting with how much garbage is created when using UniRx Switch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UniRx; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
public class SwitchGarbage : MonoBehaviour { | |
void Start() { | |
float interval = 1f; | |
// 108 B Garbage | |
Observable.Interval(TimeSpan.FromSeconds(interval)) | |
.Profile("** Switch Garbage 1") | |
.SwitchSelect(_ => Observable.Return(Time.frameCount)) | |
.Subscribe(frameCount => { | |
// Do Nothing | |
}).AddTo(this); | |
// 108 B Garbage | |
int externalVar = 10; | |
Observable.Interval(TimeSpan.FromSeconds(interval)) | |
.Profile("** Switch Garbage 2") | |
.SwitchSelect(_ => Observable.Return(Time.frameCount * externalVar)) | |
.Subscribe(frameCount => { | |
// Do Nothing | |
}).AddTo(this); | |
// 108 B Garbage | |
Observable.Interval(TimeSpan.FromSeconds(interval)) | |
.Profile("** Switch Garbage 3") | |
.SwitchSelect(_ => { | |
int internalVar = 10; | |
UnityEngine.Profiling.Profiler.BeginSample("**Observable.Return"); | |
// 20 B Garbage | |
var result = Observable.Return(Time.frameCount * internalVar); | |
UnityEngine.Profiling.Profiler.EndSample(); | |
return result; | |
}) | |
.Subscribe(frameCount => { | |
// Do Nothing | |
}).AddTo(this); | |
// 88 B Garbage | |
var externalReturn = Observable.Return(Time.frameCount); | |
Observable.Interval(TimeSpan.FromSeconds(interval)) | |
.Profile("** Switch Garbage 4") | |
.SwitchSelect(_ => externalReturn) | |
.Subscribe(frameCount => { | |
// Do Nothing | |
}).AddTo(this); | |
// 2.6 KB Garbage | |
var onNewInt = new Subject<int>(); | |
var intArray = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; | |
var newAndExistingInts = onNewInt.StartWith(intArray); | |
Observable.Interval(TimeSpan.FromSeconds(interval)) | |
.Profile("** Switch Garbage 5") | |
.SwitchSelect(v => newAndExistingInts | |
.Where(xs => xs == v) | |
.First()) | |
.Subscribe(_ => { | |
// Do Nothing | |
}).AddTo(this); | |
// 248 B Garbage | |
Observable.Interval(TimeSpan.FromSeconds(interval)) | |
.Profile("** Switch Garbage 6") | |
.Select(v => intArray | |
.Where(xs => xs == v) | |
.FirstOrDefault()) | |
.Where( xs => xs != 0 ) | |
.Subscribe(_ => { | |
// Do Nothing | |
}).AddTo(this); | |
// 0.9 KB Garbage | |
Observable.Interval(TimeSpan.FromSeconds(interval)) | |
.Profile("** Switch Garbage 7") | |
.SwitchSelect(v => onNewInt | |
.Where(xs => xs == v) | |
.First()) | |
.Subscribe(_ => { | |
// Do Nothing | |
}).AddTo(this); | |
// 0.7 KB Garbage | |
Observable.Interval(TimeSpan.FromSeconds(interval)) | |
.Profile("** Switch Garbage 8") | |
.SwitchSelect(v => onNewInt | |
.Where( xs => xs == v)) | |
.Subscribe(_ => { | |
// Do Nothing | |
}).AddTo(this); | |
// 376 B Garbage | |
Observable.Interval(TimeSpan.FromSeconds(interval)) | |
.Profile("** Switch Garbage 9") | |
.SwitchSelect(v => onNewInt ) | |
.Subscribe(_ => { | |
// Do Nothing | |
}).AddTo(this); | |
// 2.3 KB Garbage | |
Observable.Interval(TimeSpan.FromSeconds(interval)) | |
.Profile("** Switch Garbage 10") | |
.SwitchSelect(v => newAndExistingInts ) | |
.Subscribe(_ => { | |
// Do Nothing | |
}).AddTo(this); | |
// 0.5 KB | |
var onNewIntObservable1 = onNewInt.AsObservable(); | |
Observable.Interval(TimeSpan.FromSeconds(interval)) | |
.Profile("** Switch Garbage 11") | |
.SwitchSelect(v => onNewIntObservable1) | |
.Subscribe(_ => { | |
// Do Nothing | |
}).AddTo(this); | |
// 408 B | |
var onNewIntObservable2 = (IObservable<int>)onNewInt; | |
Observable.Interval(TimeSpan.FromSeconds(interval)) | |
.Profile("** Switch Garbage 12") | |
.SwitchSelect(v => onNewIntObservable2) | |
.Subscribe(_ => { | |
// Do Nothing | |
}).AddTo(this); | |
// 3.0 KB Garbage | |
Observable.Interval(TimeSpan.FromSeconds(interval)) | |
.Profile("** Switch Garbage 13") | |
.SwitchSelect(v => onNewInt | |
.Where(xs => xs == v) | |
.Where(xs => xs == v) | |
.Where(xs => xs == v) | |
.Where(xs => xs == v) | |
.Where(xs => xs == v) | |
.Where(xs => xs == v) | |
.Where(xs => xs == v) | |
.Where(xs => xs == v) | |
.Where(xs => xs == v)) | |
.Subscribe(_ => { | |
// Do Nothing | |
}).AddTo(this); | |
// 0.7 KB | |
Observable.Interval(TimeSpan.FromSeconds(interval)) | |
.Profile("** Switch Garbage 14") | |
.SwitchSelect(v => onNewInt.Select( i => Tuple.Create(v,i) ) ) | |
.Where(xs => xs.Item1 == xs.Item2) | |
.Where(xs => xs.Item1 == xs.Item2) | |
.Where(xs => xs.Item1 == xs.Item2) | |
.Where(xs => xs.Item1 == xs.Item2) | |
.Where(xs => xs.Item1 == xs.Item2) | |
.Where(xs => xs.Item1 == xs.Item2) | |
.Where(xs => xs.Item1 == xs.Item2) | |
.Where(xs => xs.Item1 == xs.Item2) | |
.Where(xs => xs.Item1 == xs.Item2) | |
.Subscribe(_ => { | |
// Do Nothing | |
}).AddTo(this); | |
// 244 B | |
Observable.Interval(TimeSpan.FromSeconds(interval)) | |
.Profile("** Switch Garbage 15") | |
.SwitchSelect(v => GetFirst(intArray, onNewInt, i => i==v ) ) | |
.Subscribe(_ => { | |
// Do Nothing | |
}).AddTo(this); | |
// 0.6 KB | |
Observable.Interval(TimeSpan.FromSeconds(interval)) | |
.Profile("** Switch Garbage 16") | |
.SwitchSelect(v => Observable.Timer(TimeSpan.FromSeconds(0.5f)) ) | |
.Subscribe(_ => { | |
// Do Nothing | |
}).AddTo(this); | |
// 501 B | |
var timer = Observable.Timer(TimeSpan.FromSeconds(0.5f)); | |
byte[] garbage = null; | |
Observable.Interval(TimeSpan.FromSeconds(interval)) | |
.Profile("** Switch Garbage 17") | |
.SwitchSelect(v => timer) | |
.Subscribe(_ => { | |
// Do Nothing | |
UnityEngine.Profiling.Profiler.BeginSample("**Debug.Log"); | |
garbage = new byte[1024]; | |
Debug.Log("test" + "a"); | |
UnityEngine.Profiling.Profiler.EndSample(); | |
}).AddTo(this); | |
BoolReactiveProperty boolProperty = new BoolReactiveProperty(true); | |
var boolTimer = Observable.Timer(TimeSpan.FromSeconds(0.5f)); | |
UnityEngine.Profiling.Profiler.BeginSample("**Bool Switch"); | |
boolProperty | |
.SwitchSelectWhenTrue(() => boolTimer) | |
.Subscribe(_ => { | |
// Do nothing | |
}).AddTo(this); | |
UnityEngine.Profiling.Profiler.EndSample(); | |
} | |
IObservable<T> GetFirst<T>( List<T> existing, IObservable<T> onNew, Func<T,bool> selector ) { | |
for( int i=0; i<existing.Count; i++ ) { | |
if( selector( existing[i] ) == true ) { | |
return Observable.Return(existing[i]); | |
} | |
} | |
return onNew | |
.Where(selector) | |
.First(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any conclusions?