Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [System.Flags]
- internal enum Foo
- {
- Bar = 0x00,
- Foof = 0x01,
- Xyzzy = 0x02,
- Baz = 0x04
- }
- private class Fubar
- {
- internal Foo foo;
- internal HasLocation location;
- }
- private static long CountBit(Fubar[] fubars, long len)
- {
- long count = 0;
- for (long i = 0; i < len; ++i)
- if ((fubars[i].foo & Foo.Xyzzy) > 0)
- ++count;
- return count;
- }
- private static long CountIs(Fubar[] fubars, long len)
- {
- long count = 0;
- for (long i = 0; i < len; ++i)
- if (fubars[i].location is Rectangle rect)
- ++count;
- return count;
- }
- [Test]
- public void TestIsVsBitFlag()
- {
- var len = Base.Cent * Base.Cent * 1000 * 10;
- var fubars = new Fubar[len];
- for (int i = 0; i < len; ++i)
- {
- if ((i % 2) == 0)
- fubars[i] = new Fubar { foo = Foo.Foof | Foo.Baz, location = Location.Zero };
- else
- fubars[i] = new Fubar { foo = Foo.Xyzzy, location = Rectangle.CreateRectangle(0, 0, 0, 0) };
- }
- GC.Collect();
- long bitAxx = 0L;
- {
- var watch = System.Diagnostics.Stopwatch.StartNew();
- long count = CountBit(fubars, len: len);
- bitAxx = watch.ElapsedMilliseconds;
- }
- long isAxx = 0L;
- {
- var watch = System.Diagnostics.Stopwatch.StartNew();
- long count = CountIs(fubars, len: len);
- isAxx = watch.ElapsedMilliseconds;
- }
- Assert.That(isAxx, Is.LessThan(bitAxx), "Got isAxx: {0} ms vs bitAxx: {1} ms.", isAxx, bitAxx);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement