Skip to content

Instantly share code, notes, and snippets.

@descorp
Last active October 20, 2017 09:16
Show Gist options
  • Save descorp/03578ea03bdbeba7578110eeb4a01d69 to your computer and use it in GitHub Desktop.
Save descorp/03578ea03bdbeba7578110eeb4a01d69 to your computer and use it in GitHub Desktop.
Xamarin iOS Array profiler
public interface IProfilable {
TimeSpan MeasureWrite();
TimeSpan MeasureRead();
TimeSpan MeasureFind();
}
public class Result {
public Measurement[] measurements;
public Result(params Measurement[] array) {
measurements = array;
}
override public string ToString() {
var str = "";
foreach(var item in measurements) {
str += item.ToString() + "\r\n\r\n";
}
return str;
}
}
public struct Measurement {
private int count;
private string name;
private TimeSpan total;
public TimeSpan Max;
public TimeSpan Min;
public TimeSpan Total {
get {
return total;
}
set {
total = value;
count++;
}
}
public TimeSpan Avg {
get {
return TimeSpan.FromTicks(Total.Ticks / count);
}
}
override public string ToString() {
return $" Name . . . . {name}\r\n Min. . . . . {Min}\r\n Avg. . . . . {Avg}\r\n Max. . . . . {Max}\r\n Total. . . . {total}";
}
public Measurement(string name, Func<TimeSpan> measuring, int n) {
this.name = name;
count = 0;
total = TimeSpan.Zero;
Max = TimeSpan.MinValue;
Min = TimeSpan.MaxValue;
for(var i = 0; i < n; i++) {
var round = measuring();
if(Max < round) {
Max = round;
}
if(Min > round) {
Min = round;
}
Total += round;
}
}
}
public static Result Measure(IProfilable sut, int n) {
var result = new Result(
new Measurement("Write", sut.MeasureWrite, n),
new Measurement("Read", sut.MeasureRead, 100),
new Measurement("Find", sut.MeasureFind, 100));
return result;
}
public class NSMutableArrayProfiler: IProfilable {
NSString item = new NSString("42");
NSMutableArray<NSString> array;
public NSMutableArrayProfiler(int n = 0) {
array = new NSMutableArray<NSString>((nuint)n);
}
public TimeSpan MeasureWrite() {
var start = DateTime.Now;
array.Add(item);
return DateTime.Now - start;
}
public TimeSpan MeasureRead() {
var index = new Random().Next(0, (int)array.Count);
var start = DateTime.Now;
var temp = array[(nuint)index];
return DateTime.Now - start;
}
public TimeSpan MeasureFind() {
var start = DateTime.Now;
var temp = array.FirstOrDefault(x => x != item);
return DateTime.Now - start;
}
}
public class ListProfiler: IProfilable {
List<string> array;
string item = "item";
public ListProfiler(int n = 0) {
array = new List<string>(n);
if(n > 0) {
array.Capacity = n;
}
}
public TimeSpan MeasureWrite() {
var start = DateTime.Now;
array.Add(item);
return DateTime.Now - start;
}
public TimeSpan MeasureRead() {
var index = new Random().Next(0, array.Count);
var start = DateTime.Now;
var temp = array[index];
return DateTime.Now - start;
}
public TimeSpan MeasureFind() {
var start = DateTime.Now;
var temp = array.FirstOrDefault(x => x != item);
return DateTime.Now - start;
}
}
static void Measure(int n) {
var profilables = new Dictionary<string, IProfilable>() {
["ListProfiler(0)"] = new ListProfiler(),
["ListProfiler(n)"] = new ListProfiler(n),
["NSMutableArrayProfiler(0)"] = new NSMutableArrayProfiler(),
["NSMutableArrayProfiler(n)"] = new NSMutableArrayProfiler(n)
};
Console.WriteLine($"\r\n _____________________ \r\n Measure {n}");
foreach(var item in profilables) {
Console.WriteLine($"\r\n{item.Key}:");
Console.WriteLine(Measure(item.Value, n));
}
}
Measure(1000);
Measure(10000);
Measure(100000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment