Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azyobuzin/a6d46bbe4cff5915139eef77beaa0f3d to your computer and use it in GitHub Desktop.
Save azyobuzin/a6d46bbe4cff5915139eef77beaa0f3d to your computer and use it in GitHub Desktop.
BenchmarkDotNet=v0.11.3, OS=Windows 10.0.17763.292 (1809/October2018Update/Redstone5)
Intel Core i7-3770 CPU 3.40GHz (Ivy Bridge), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=2.2.101
  [Host] : .NET Core 2.2.0 (CoreCLR 4.6.27110.04, CoreFX 4.6.27110.04), 64bit RyuJIT

Job=InProcess  Toolchain=InProcessToolchain  
Method ElementCount Mean Error StdDev
FindInteger 10 5.922 us 0.0844 us 0.0790 us
WhereInteger 10 308.913 us 3.3159 us 2.9394 us
FindString 10 6.377 us 0.1244 us 0.1163 us
WhereString 10 265.993 us 2.1633 us 1.8065 us
FindInteger 100 5.762 us 0.0677 us 0.0600 us
WhereInteger 100 309.874 us 1.8623 us 1.5551 us
FindString 100 6.288 us 0.1025 us 0.0959 us
WhereString 100 264.207 us 1.7637 us 1.4728 us
FindInteger 1000 5.873 us 0.0839 us 0.0785 us
WhereInteger 1000 309.562 us 4.1908 us 3.7150 us
FindString 1000 6.361 us 0.1068 us 0.0999 us
WhereString 1000 266.667 us 1.8926 us 1.6778 us
FindInteger 10000 6.122 us 0.0621 us 0.0581 us
WhereInteger 10000 311.376 us 1.5438 us 1.3685 us
FindString 10000 6.570 us 0.0977 us 0.0914 us
WhereString 10000 266.397 us 2.6091 us 2.3129 us
FindInteger 100000 6.049 us 0.1136 us 0.1007 us
WhereInteger 100000 338.498 us 5.7355 us 4.7894 us
FindString 100000 6.470 us 0.0325 us 0.0288 us
WhereString 100000 266.489 us 2.3454 us 1.9586 us
FindInteger 200000 6.111 us 0.0776 us 0.0726 us
WhereInteger 200000 371.447 us 6.0218 us 5.6328 us
FindString 200000 6.722 us 0.0997 us 0.0833 us
WhereString 200000 266.061 us 1.3401 us 1.1190 us
FindInteger 400000 6.157 us 0.0808 us 0.0716 us
WhereInteger 400000 434.017 us 3.2092 us 2.8449 us
FindString 400000 6.550 us 0.0983 us 0.0919 us
WhereString 400000 266.135 us 1.6292 us 1.5240 us
FindInteger 600000 6.156 us 0.0832 us 0.0778 us
WhereInteger 600000 501.401 us 8.0570 us 7.1424 us
FindString 600000 6.621 us 0.1053 us 0.0985 us
WhereString 600000 266.532 us 1.8006 us 1.5962 us
FindInteger 800000 6.077 us 0.0766 us 0.0679 us
WhereInteger 800000 558.154 us 2.8385 us 2.5163 us
FindString 800000 6.535 us 0.0676 us 0.0599 us
WhereString 800000 265.124 us 1.4133 us 1.3220 us
FindInteger 1000000 6.139 us 0.0512 us 0.0453 us
WhereInteger 1000000 635.331 us 12.0108 us 11.7962 us
FindString 1000000 6.729 us 0.1687 us 0.1578 us
WhereString 1000000 269.503 us 4.6844 us 4.3817 us
using Realms;
namespace FindBenchmark
{
public class IntegerObject : RealmObject
{
[PrimaryKey]
public long Key { get; set; }
}
}
using System;
using System.IO;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Realms;
namespace FindBenchmark
{
class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<FindBenchmarkClass>();
}
private static void ReferenceAssembly()
{
// アセンブリロードに失敗したとは言わせない
Realm.GetInstance()
.All<IntegerObject>()
.Where(x => true)
.ToList();
}
}
[InProcess]
public class FindBenchmarkClass
{
[Params(10, 100, 1000, 10000, 100000, 200000, 400000, 600000, 800000, 1000000)]
public int ElementCount { get; set; }
private Realm _realm;
private long _integerKey;
private string _stringKey;
[GlobalSetup]
public void GlobalSetup()
{
var dbFileDirectory = Environment.GetEnvironmentVariable("BENCHMARK_DB_DIR");
if (string.IsNullOrEmpty(dbFileDirectory))
dbFileDirectory = Directory.GetCurrentDirectory();
var dbFilePath = Path.Join(dbFileDirectory, $"FindBenchmark{this.ElementCount:D}.realm");
var writableRealmConfig = new RealmConfiguration(dbFilePath) { ShouldDeleteIfMigrationNeeded = true };
var written = false;
using (var realm = Realm.GetInstance(writableRealmConfig))
{
if (realm.All<IntegerObject>().Count() != this.ElementCount)
{
realm.Write(() =>
{
realm.RemoveAll<IntegerObject>();
for (var i = 0; i < this.ElementCount; i++)
{
realm.Add(new IntegerObject() { Key = i });
}
});
written = true;
}
if (realm.All<StringObject>().Count() != this.ElementCount)
{
realm.Write(() =>
{
realm.RemoveAll<StringObject>();
for (var i = 0; i < this.ElementCount; i++)
{
realm.Add(new StringObject() { Key = i.ToString("D") });
}
});
written = true;
}
}
if (written) Realm.Compact(writableRealmConfig);
this._realm = Realm.GetInstance(new RealmConfiguration(dbFilePath)
{
IsReadOnly = true,
});
// 最後から2番目を検索対象にする
this._integerKey = this._realm.All<IntegerObject>()
.AsEnumerable()
.Reverse()
.ElementAt(1)
.Key;
this._stringKey = this._realm.All<StringObject>()
.AsEnumerable()
.Reverse()
.ElementAt(1)
.Key;
}
[GlobalCleanup]
public void GlobalCleanup()
{
if (this._realm != null)
{
this._realm.Dispose();
this._realm = null;
}
}
[Benchmark]
public IntegerObject FindInteger()
{
return this._realm.Find<IntegerObject>(this._integerKey);
}
[Benchmark]
public IntegerObject WhereInteger()
{
return this._realm.All<IntegerObject>()
.First(x => x.Key == this._integerKey);
}
[Benchmark]
public StringObject FindString()
{
return this._realm.Find<StringObject>(this._stringKey);
}
[Benchmark]
public StringObject WhereString()
{
return this._realm.All<StringObject>()
.First(x => x.Key == this._stringKey);
}
}
}
using Realms;
namespace FindBenchmark
{
public class StringObject : RealmObject
{
[PrimaryKey]
public string Key { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment