Example #1
0
        public static void Run()
        {
            var x = 0;

            const int iterations = 10000000;

            Stopwatch sw = Stopwatch.StartNew();

            // x递增1000w,需要多长时间
            for (int i = 0; i < iterations; i++)
            {
                x++;
            }

            Console.WriteLine($"Incrementing x: {sw.ElapsedMilliseconds:N0}");

            sw.Restart();

            // 加上一个调用什么都不做的方法的开销,需要多久
            for (int i = 0; i < iterations; i++)
            {
                Nothing();
                x++;
                Nothing();
            }

            Console.WriteLine($"Incrementing x in Nothing: {sw.ElapsedMilliseconds:N0}");

            // 加上一个无竞争的SpinLock的开销,需要多久
            SpinLock sl = new SpinLock(false);

            sw.Restart();

            for (int i = 0; i < iterations; i++)
            {
                bool taken = false;
                sl.Enter(ref taken);
                x++;
                sl.Exit();
            }

            Console.WriteLine($"Incrementing x in SpinLock: {sw.ElapsedMilliseconds:N0}");

            // 加上调用一个无竞争的SimpleWaitLock的开销,需要多久

            using (SimpleWaitLock swl = new SimpleWaitLock())
            {
                sw.Restart();

                for (int i = 0; i < iterations; i++)
                {
                    swl.Enter();
                    x++;
                    swl.Leave();
                }

                Console.WriteLine($"Incrementing x in SimpleWaitLock: {sw.ElapsedMilliseconds:N0}");
            }
        }
    public static void Go()
    {
        Int32       x          = 0;
        const Int32 iterations = 10000000; // 10 million

        // How long does it take to increment x 10 million times?
        Stopwatch sw = Stopwatch.StartNew();

        for (Int32 i = 0; i < iterations; i++)
        {
            x++;
        }
        Console.WriteLine("Incrementing x: {0:N0}", sw.ElapsedMilliseconds);

        // How long does it take to increment x 10 million times
        // adding the overhead of calling a method that does nothing?
        sw.Restart();
        for (Int32 i = 0; i < iterations; i++)
        {
            M(); x++; M();
        }
        Console.WriteLine("Incrementing x in M: {0:N0}", sw.ElapsedMilliseconds);

        // How long does it take to increment x 10 million times
        // adding the overhead of calling an uncontended SimpleSpinLock?
        SimpleSpinLock ssl = new SimpleSpinLock();

        sw.Restart();
        for (Int32 i = 0; i < iterations; i++)
        {
            ssl.Enter(); x++; ssl.Leave();
        }
        Console.WriteLine("Incrementing x in SimpleSpinLock: {0:N0}", sw.ElapsedMilliseconds);

        // How long does it take to increment x 10 million times
        // adding the overhead of calling an uncontended SpinLock?
        SpinLock sl = new SpinLock(false);

        sw.Restart();
        for (Int32 i = 0; i < iterations; i++)
        {
            Boolean taken = false;
            sl.Enter(ref taken); x++; sl.Exit(false);
        }
        Console.WriteLine("Incrementing x in SpinLock: {0:N0}", sw.ElapsedMilliseconds);

        // How long does it take to increment x 10 million times
        // adding the overhead of calling an uncontended SimpleWaitLock?
        using (SimpleWaitLock swl = new SimpleWaitLock()) {
            sw.Restart();
            for (Int32 i = 0; i < iterations; i++)
            {
                swl.Enter(); x++; swl.Leave();
            }
            Console.WriteLine("Incrementing x in SimpleWaitLock: {0:N0}", sw.ElapsedMilliseconds);
        }
        Console.ReadLine();
    }
Example #3
0
 public LockTestSuits()
 {
     _resource     = 0;
     sslock        = new SimpleSpinLock();
     swlock        = new SimpleWaitLock();
     semaphorelock = new SimpleWaitWithSemaphoreLock();
     mutex         = new Mutex();
     recursivelock = new RecursiveAutoResetEventLock();
 }
Example #4
0
        static void Main()
        {
#pragma warning disable 219
            int x = 0;
#pragma warning restore 219
            const int iterations = 10000000; // 10 миллионов

            // Note: Сколько времени займет инкремент x 10 миллионов раз?
            Stopwatch stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < iterations; i++)
            {
                x++;
            }
            Console.WriteLine("Incrementing x: {0:N0}", stopwatch.ElapsedMilliseconds);

            // Note: Сколько времени займет инкремент x 10 миллионов раз, если
            // добавить вызов ничего не делающего метода?
            stopwatch.Restart();
            for (int i = 0; i < iterations; i++)
            {
                Dummy();
                x++;
                Dummy();
            }
            Console.WriteLine("Incrementing x in Dummy: {0:N0}", stopwatch.ElapsedMilliseconds);

            // Note: Сколько времени займет инкремент x 10 миллионов раз, если
            // добавить вызов неконкурирующего объекта SpinLock
            var spinLock = new SpinLock();
            stopwatch.Restart();
            for (int i = 0; i < iterations; i++)
            {
                bool lockTaken = false;
                spinLock.Enter(ref lockTaken);
                x++;
                spinLock.Exit();
            }
            Console.WriteLine("Incrementing x in SpinLock: {0:N0}", stopwatch.ElapsedMilliseconds);

            // Note: Сколько времени займет инкремент x 10 миллионов раз, если
            // добавить вызов неконкурирующего объекта SimpleWaitLock?
            using (var waitLock = new SimpleWaitLock())
            {
                stopwatch.Restart();
                for (int i = 0; i < iterations; i++)
                {
                    waitLock.Enter();
                    x++;
                    waitLock.Leave();
                }
                Console.WriteLine("Incrementing x in SimpleWaitLock: {0:N0}", stopwatch.ElapsedMilliseconds);
            }
        }
Example #5
0
        public static void Go()
        {
            int       x          = 0;
            const int iterations = 1000000;
            Stopwatch sw         = Stopwatch.StartNew();

            for (int i = 0; i < iterations; i++)
            {
                x++;
            }
            Console.WriteLine("Incrementing x: {0:N0}", sw.ElapsedMilliseconds);

            sw.Restart();
            for (int i = 0; i < iterations; i++)
            {
                M(); x++; M();
            }
            Console.WriteLine("Incrementing x in M: {0:N0}", sw.ElapsedMilliseconds);

            SimpleSpinLock ssl = new SimpleSpinLock();

            sw.Restart();
            for (int i = 0; i < iterations; i++)
            {
                ssl.Enter(); x++; ssl.Leave();
            }
            Console.WriteLine("Incrementing x in SimpleSpinLock:{0:N0}", sw.ElapsedMilliseconds);

            SpinLock sl = new SpinLock(false);

            sw.Restart();
            for (int i = 0; i < iterations; i++)
            {
                bool taken = false;
                sl.Enter(ref taken); x++; sl.Exit(false);
            }
            Console.WriteLine("Incrementing x in SpinLock: {0:N0}", sw.ElapsedMilliseconds);

            using (SimpleWaitLock swl = new SimpleWaitLock())
            {
                sw.Restart();
                for (int i = 0; i < iterations; i++)
                {
                    swl.Enter(); x++; swl.Leave();
                }
                Console.WriteLine("Incrementing x in SimpleWaitLock: {0:N0}", sw.ElapsedMilliseconds);
            }
            Console.ReadLine();
        }
Example #6
0
        public static void Go()
        {
            int       x          = 0;
            const int iterations = 10000000; // 10 million

            Stopwatch sw = Stopwatch.StartNew();

            for (Int32 i = 0; i < iterations; i++)
            {
                x++;
            }
            Console.WriteLine("Incrementing x: {0:N0}", sw.ElapsedMilliseconds);

            // How long does it take to increment x 10 million times
            // adding the overhead of calling a method that does nothing?
            sw.Restart();
            for (Int32 i = 0; i < iterations; i++)
            {
                M(); x++; M();
            }
            Console.WriteLine("Incrementing x in M: {0:N0}", sw.ElapsedMilliseconds);

            // How long does it take to increment x 10 million times
            // adding the overhead of calling an uncontended SpinLock?
            SpinLock sl = new SpinLock(false);

            sw.Restart();
            for (Int32 i = 0; i < iterations; i++)
            {
                Boolean taken = false;
                sl.Enter(ref taken); x++; sl.Exit(false);
            }
            Console.WriteLine("Incrementing x in SpinLock: {0:N0}", sw.ElapsedMilliseconds);

            // How long does it take to increment x 10 million times
            var shl = new SimpleHybridLock();

            shl.Enter();
            x++;
            shl.Leave();

            sw.Restart();
            for (int i = 0; i < iterations; i++)
            {
                shl.Enter();
                x++;
                shl.Leave();
            }
            Console.WriteLine("Incrementing x in SimpleHybridLock: {0:N0}", sw.ElapsedMilliseconds);

            using (var ahl = new AnotherHybridLock())
            {
                ahl.Enter();
                x++;
                ahl.Leave();
                sw.Restart();
                for (int i = 0; i < iterations; i++)
                {
                    ahl.Enter();
                    x++;
                    ahl.Leave();
                }
                Console.WriteLine("Incrementing x in AnotherHybridLock: {0:N0}", sw.ElapsedMilliseconds);
            }

            using (SimpleWaitLock swl = new SimpleWaitLock())
            {
                sw.Restart();
                for (Int32 i = 0; i < iterations; i++)
                {
                    swl.Enter(); x++; swl.Leave();
                }
                Console.WriteLine("Incrementing x in SimpleWaitLock: {0:N0}", sw.ElapsedMilliseconds);
            }

            //using (var oml = new OneManyLock())
            //{

            //}

            //using (var rwls = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion))
            //{
            //    rwls
            //}
        }
   public static void Go() {
      Int32 x = 0;
      const Int32 iterations = 10000000;  // 10 million

      // How long does it take to increment x 10 million times?
      Stopwatch sw = Stopwatch.StartNew();
      for (Int32 i = 0; i < iterations; i++) {
         x++;
      }
      Console.WriteLine("Incrementing x: {0:N0}", sw.ElapsedMilliseconds);

      // How long does it take to increment x 10 million times 
      // adding the overhead of calling a method that does nothing?
      sw.Restart();
      for (Int32 i = 0; i < iterations; i++) {
         M(); x++; M();
      }
      Console.WriteLine("Incrementing x in M: {0:N0}", sw.ElapsedMilliseconds);

      // How long does it take to increment x 10 million times 
      // adding the overhead of calling an uncontended SimpleSpinLock?
      SimpleSpinLock ssl = new SimpleSpinLock();
      sw.Restart();
      for (Int32 i = 0; i < iterations; i++) {
         ssl.Enter(); x++; ssl.Leave();
      }
      Console.WriteLine("Incrementing x in SimpleSpinLock: {0:N0}", sw.ElapsedMilliseconds);

      // How long does it take to increment x 10 million times 
      // adding the overhead of calling an uncontended SpinLock?
      SpinLock sl = new SpinLock(false);
      sw.Restart();
      for (Int32 i = 0; i < iterations; i++) {
         Boolean taken = false;
         sl.Enter(ref taken); x++; sl.Exit(false);
      }
      Console.WriteLine("Incrementing x in SpinLock: {0:N0}", sw.ElapsedMilliseconds);

      // How long does it take to increment x 10 million times 
      // adding the overhead of calling an uncontended SimpleWaitLock?
      using (SimpleWaitLock swl = new SimpleWaitLock()) {
         sw.Restart();
         for (Int32 i = 0; i < iterations; i++) {
            swl.Enter(); x++; swl.Leave();
         }
         Console.WriteLine("Incrementing x in SimpleWaitLock: {0:N0}", sw.ElapsedMilliseconds);
      }
      Console.ReadLine();
   }