WPF中多线程统计拆箱装箱和泛型的执行效率

WPF中多线程统计拆箱装箱和泛型的执行效率,使用的知识点有泛型、多线程、委托,从例子中可以看到使用泛型的效率至少提升2倍

MainWindow.xaml

<Window x:Class="Box.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <TextBlock FontSize="12" HorizontalAlignment="Left" Margin="30" Text="装箱、开箱:"></TextBlock>
        <TextBlock FontSize="12" Text="0" Margin="30" Name="InformationOne" Grid.Column="1" Grid.Row="0"></TextBlock>
        <TextBlock FontSize="12" HorizontalAlignment="Left" Margin="30" Text="使用泛型:" Grid.Column="0" Grid.Row="1"></TextBlock>
        <TextBlock FontSize="12" Text="0"  Margin="30" Name="InformationTwo" Grid.Column="1" Grid.Row="1"></TextBlock>
        <Button FontSize="20" HorizontalAlignment="Stretch" Margin="10" Name="Button"  Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="3" Content="开始" Click="Button_Click"></Button>

    </Grid>
</Window>

MainWindow.xaml.cs

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 委托对象
        /// </summary>
        /// <param name="completed">完成次数</param>
        public delegate void CallBackDelegate(int completed);
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 回调方法
        /// </summary>
        /// <param name="completed">完成次数</param>
        private void CallBack(int completed)
        {
            MessageBox.Show(string.Format("子线程通知主线程:运行完成,线程执行{0}次!", completed));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Total total = new Total();
            total.informationOne = InformationOne;
            total.informationTwo = InformationTwo;
            InformationOne.Text = "0";
            InformationTwo.Text = "0";
            total.button = Button;
            CallBackDelegate handler = CallBack;
            total.callBack = handler;

            for (int i = 0; i < 2; i++)
            {
                Thread thread = new Thread(new ParameterizedThreadStart(total.TotalNumber));
                thread.IsBackground = true;
                thread.Start(i);
            }
            Button.Visibility = System.Windows.Visibility.Hidden;
        }
    }

    public class Total
    {
        /// <summary>
        /// 显示拆箱、装箱的TextBlock
        /// </summary>
        public TextBlock informationOne;

        /// <summary>
        /// 显示泛型的TextBlock
        /// </summary>
        public TextBlock informationTwo;

        /// <summary>
        /// 用来控制按钮的显示和隐藏
        /// </summary>
        public Button button;
        /// <summary>
        /// 委托对象
        /// </summary>
        public object callBack;

        /// <summary>
        /// 总的运行次数
        /// </summary>
        private int times = 10000000;

        /// <summary>
        /// 线程的访问次数
        /// </summary>
        private int completed = 0;

        public void TotalNumber(object obj)
        {
            lock (typeof(Total))
            {
                //把传来的参数转换为委托
                MainWindow.CallBackDelegate handler = callBack as MainWindow.CallBackDelegate;
                if (obj.Equals(0))
                {
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    ArrayList array = new ArrayList();
                    for (int i = 0; i < times; i++)
                    {
                        array.Add(i);//装箱
                    }
                    int m = 0;
                    foreach (int i in array)//拆箱
                    {
                        if (i % 1000 == 0 || i >= times)
                        {
                            informationOne.Dispatcher.Invoke(
                                new Action(
                                     delegate
                                     {
                                         informationOne.Text = m.ToString();
                                     }
                                )
                            );
                        }
                        m++;
                    }
                    watch.Stop();
                    //watch.Reset();

                    string infoOne = string.Format("装箱、开箱耗时:{1}毫秒", m, watch.ElapsedMilliseconds);
                    informationOne.Dispatcher.Invoke(
                        new Action(
                             delegate
                             {
                                 informationOne.Text = infoOne;
                             }
                        )
                    );
                }
                else
                {
                    DateTime startTime = DateTime.Now;
                    List<int> list = new List<int>();
                    for (int i = 0; i < times; i++)
                    {
                        list.Add(i);
                    }
                    int n = 0;
                    foreach (int i in list)
                    {
                        if (i % 1000 == 0 || i >= times)
                        {
                            informationTwo.Dispatcher.Invoke(
                                new Action(
                                     delegate
                                     {
                                         informationTwo.Text = n.ToString();
                                     }
                                )
                            );
                        }
                        n++;
                    }
                    TimeSpan timeSpan = DateTime.Now - startTime;
                    string infoTwo = string.Format("使用泛型耗时:{1}毫秒", n, (int)timeSpan.TotalMilliseconds);
                    informationTwo.Dispatcher.Invoke(
                        new Action(
                             delegate
                             {
                                 informationTwo.Text = infoTwo;
                             }
                        )
                    );
                }
                completed++;
                if (completed >= 2)
                {
                    button.Dispatcher.Invoke(
                        new Action(
                             delegate
                             {
                                 button.Visibility = Visibility.Visible;
                             }
                        )
                    );
                    handler(completed);
                }
            }
        }
    }

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-24 14:04:59

WPF中多线程统计拆箱装箱和泛型的执行效率的相关文章

WPF中多线程统计拆箱装箱和泛型的运行效率

WPF中多线程统计拆箱装箱和泛型的执行效率.使用的知识点有泛型.多线程.托付.从样例中能够看到使用泛型的效率至少提升2倍 MainWindow.xaml <Window x:Class="Box.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xa

拆箱装箱 页面跳转 及内存管理 0905蓝懿教育

拆箱装箱 数组中只能装入对象,而很多时候我们需要把类似Int 或者结构体这种装入 此时我们需要进行拆装 基本思想是 把类似int还有结构体 转换成相对应的对象 然后加入数组 ,导出时 要从对象型变成对应的int或者结构体类 @property NSmutableArray *ages; self.ages=[NSmutableArray  Array]; int x=1; NSnumber *objectnumber=x; [self.ages addobject:objectnumber];

java自动拆箱装箱易导致的两个错误

自J2SE 5.0开始提供的基本数据类型的自动装箱(autoboxing).拆箱(unboxing)功能. 何为自动装箱: 当我们创建一个Integer对象时,却可以这样: Integer i = 100; (注意:不是 int i = 100; ) 实际上,执行上面那句代码的时候,系统为我们执行了:Integer i = new Integer(100); 此即基本数据类型的自动装箱功能. 何为自动拆箱 自动拆箱(unboxing),也就是将对象中的基本数据从对象中自动取出.如下可实现自动拆箱

关于Java自动拆箱装箱中的缓存问题

1 package cn.zhang.test; 2 /** 3 * 测试自动装箱拆箱 4 * 自动装箱:基本类型自动转为包装类对象 5 * 自动拆箱:包装类对象自动转化为基本数据类型 6 * 7 * 8 * /*缓存问题*/ 9 /*缓存[-128,127]之间的数字,也就是一个byte,实际上是系统在初始的时候创建了一个范围在[-128,127]之间的一个数组 10 * 当我们调用valueOf的时候,首先判断该数字是否在[-128,127]之间,如果在,则在数组中拿出该对象,侧面印证了数组

拆箱装箱

1.      装箱和拆箱是一个抽象的概念 2.      装箱是将值类型转换为引用类型 :拆箱是将引用类型转换为值类型       利用装箱和拆箱功能,可通过允许值类型的任何值与Object 类型的值相互转换,将值类型与引用类型链接起来 例如: int val = 100; object obj = val; Console.WriteLine (“对象的值 = {0}", obj); 这是一个装箱的过程,是将值类型转换为引用类型的过程 int val = 100; object obj =

java学习笔记——自动拆箱装箱(Autoboxing&amp;Unboxing)

一.基本类型打包器 1.基本类型:long.int.double.float.boolean 2.类类型:Long.Integer.Double.Float.Boolean 区别:基本类型效率更高,类类型的对象却可以携带更多的信息. public class TestInteger01 { public static void main(String[] args) { int a = 10; int b = 20; Integer A = new Integer(a); Integer B =

Java之集合初探(二)Iterator(迭代器),collections,打包/解包(装箱拆箱),泛型(Generic),comparable接口

Iterator(迭代器) 所有实现了Collection接口的容器都有一个iterator方法, 用来返回一个实现了Iterator接口的对象 Iterator对象称作迭代器, 用来方便的实现对容器内的元素的遍历 迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构.迭代器通常被称为"轻量级"对象,因为创建它的代价小. Java中的Iterator功能比较简单,并且只能单向移动: (1) 使用方法iterator()要求容器返回一个I

JAVA高级特性--自动拆箱-装箱,枚举类型

基本数据类型转换为引用类型对象 一个自动装箱的例子 Integer i=10; 相当于 Integer i=new Integer(10); 一个自动拆箱的例子 Integer m=10; int n=m; 相当于n=m.intValue(); 枚举类型 所有枚举类型都继承了Enum类 枚举值都是public  static final 的  ,也就是常量,因此枚举值应大写 枚举构造器都是私有化的 //当jvm去加载使用枚举类的时候,会预先创建多个枚举类型的对象供外部使用 public stat

java拆箱装箱中的一个问题

Integer i1 = 129; Integer i2 = 129; System.out.println(i1 == i2); Integer i3 = 1; Integer i4 = 1; System.out.println(i3 == i4); Integer i5 = new Integer(1); Integer i6 = new Integer(1); System.out.println(i5 == i6); System.out.println(i5.equals(i6));