C# unsaft fixed

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace UnsafeDemo
{
    class Program
    {
        static unsafe void Main(string[] args)
        {
            #region
            //Console.WriteLine("Size of CurrentStruct struct is "+sizeof(CurrentStruct));
            //CurrentStruct amount1, amount2;
            //CurrentStruct* pAmount = &amount1;
            //long* pDollars = &(pAmount->Dollars);
            //byte* pCents = &(pAmount->Cents);

            //Console.WriteLine("Address of amount1 is 0x{0:X}",(uint)&amount1);
            //Console.WriteLine("Address of amount2 is 0x{0:X}",(uint)&amount2);
            //Console.WriteLine("Address of pAmount is 0X{0:X}",(uint)&pAmount);
            //Console.WriteLine("Address of pDollars is 0X{0:X}",(uint)&pDollars);
            //Console.WriteLine("Address of pCents is 0X{0:X}",(uint)&pCents);


            //pAmount->Dollars = 20;
            //*pCents = 50;

            //Console.WriteLine("{0} ", (uint)pCents);

            //CurrentStruct* pTempCurrency = (CurrentStruct*)pCents;

            //pCents = (byte*)(--pTempCurrency);
            //Console.WriteLine("{0}",(uint)pCents);
            #endregion

            #region
            //Console.WriteLine();
            //CurrentClass amount3=new CurrentClass();
            //fixed(long *pDollars2=&(amount3.Dollars))
            //fixed (byte* pCents2 = &(amount3.Cents))
            //{
            //    Console.WriteLine("0X{0:X}",(uint)pDollars2);
            //    Console.WriteLine("0X{0:X}", (uint)pCents2);
            //    *pDollars2 = -100;
            //    Console.WriteLine(""+amount3);
            //}
            #endregion

            Stopwatch watch1 = Stopwatch.StartNew();
            int* pInt = stackalloc int[20];
            for (int i = 0; i < 20; i++)
            {
                pInt[i] = i;
            }
            //while (*pInt < 20)
            //{

            //    Console.WriteLine(*pInt);
            //    pInt++;
            //}

            for (int i = 0; i < 20; i++)
            {
                Console.WriteLine(pInt[i]);
            }
            watch1.Stop();
            Console.WriteLine(watch1.Elapsed.ToString());

            Stopwatch watch2 = Stopwatch.StartNew();
            int[] arr = new int[20];
            for (int i = 0; i < 20; i++)
            {
                arr[i] = i;
            }
            for (int i = 0; i < 20; i++)
            {
                Console.WriteLine(arr[i]);
            }
            watch2.Stop();
            Console.WriteLine(watch2.Elapsed.ToString());
            Console.ReadKey();
        }
    }
    internal struct CurrentStruct
    {
        public long Dollars;
        public byte Cents;
        public override string ToString()
        {
            return "$" + Dollars + "." + Cents;
        }
    }

    internal class CurrentClass
    {
        public long Dollars;
        public byte Cents;
        public override string ToString()
        {
            return "$" + Dollars + "." + Cents;
        }
    }
}

 

posted on 2013-03-26 23:12  R.Ray  阅读(148)  评论(0)    收藏  举报

导航