Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.45 KB | None | 0 0
  1. [System.Flags]
  2.         internal enum Foo
  3.         {
  4.             Bar = 0x00,
  5.             Foof = 0x01,
  6.             Xyzzy = 0x02,
  7.             Baz = 0x04
  8.         }
  9.  
  10.         private class Fubar
  11.         {
  12.             internal Foo foo;
  13.             internal HasLocation location;
  14.         }
  15.  
  16.         private static long CountBit(Fubar[] fubars, long len)
  17.         {
  18.             long count = 0;
  19.             for (long i = 0; i < len; ++i)
  20.                 if ((fubars[i].foo & Foo.Xyzzy) > 0)
  21.                     ++count;
  22.             return count;
  23.         }
  24.  
  25.         private static long CountIs(Fubar[] fubars, long len)
  26.         {
  27.             long count = 0;
  28.             for (long i = 0; i < len; ++i)
  29.                 if (fubars[i].location is Rectangle rect)
  30.                     ++count;
  31.             return count;
  32.         }
  33.  
  34.         [Test]
  35.         public void TestIsVsBitFlag()
  36.         {
  37.             var len = Base.Cent * Base.Cent * 1000 * 10;
  38.             var fubars = new Fubar[len];
  39.             for (int i = 0; i < len; ++i)
  40.             {
  41.                 if ((i % 2) == 0)
  42.                     fubars[i] = new Fubar { foo = Foo.Foof | Foo.Baz, location = Location.Zero };
  43.                 else
  44.                     fubars[i] = new Fubar { foo = Foo.Xyzzy, location = Rectangle.CreateRectangle(0, 0, 0, 0) };
  45.  
  46.             }
  47.  
  48.             GC.Collect();
  49.  
  50.             long bitAxx = 0L;
  51.             {
  52.                 var watch = System.Diagnostics.Stopwatch.StartNew();
  53.                 long count = CountBit(fubars, len: len);
  54.                 bitAxx = watch.ElapsedMilliseconds;
  55.             }
  56.  
  57.             long isAxx = 0L;
  58.             {
  59.                 var watch = System.Diagnostics.Stopwatch.StartNew();
  60.                 long count = CountIs(fubars, len: len);
  61.                 isAxx = watch.ElapsedMilliseconds;
  62.             }
  63.  
  64.             Assert.That(isAxx, Is.LessThan(bitAxx), "Got isAxx: {0} ms vs bitAxx: {1} ms.", isAxx, bitAxx);
  65.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement