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);
}
}
}
}

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

  1. Java中的自动拆箱装箱(Autoboxing&Unboxing)

    一.基本类型打包器 1.基本类型:long.int.double.float.boolean 2.类类型:Long.Integer.Double.Float.Boolean 区别:基本类型效率更高,类 ...

  2. 如何理解Java中的自动拆箱和自动装箱?

    小伟刚毕业时面的第一家公司就被面试官给问住了... 如何理解Java中的自动拆箱和自动装箱? 自动拆箱?自动装箱?什么鬼,听都没听过啊,这...这..知识盲区... 回到家后小伟赶紧查资料,我透,这不 ...

  3. Java包装类及其拆箱装箱

    Java包装类,Wrapper~由于在java中,数据类型总共可分为两大种,基本数据类型(值类型)和类类型(引用数据类型).基本类型的数据不是对象,所以对于要将数据类型作为对象来使用的情况,java提 ...

  4. Java 从Character和char的区别来学习自动拆箱装箱

    本文结构 1.Character和char 的区别: 2.自动拆箱装箱 1.Character和char 的区别: Character是类,char基本数据类型. 在java中有三个类负责对字符的操作 ...

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

    package cn.zhang.test; /** * 测试自动装箱拆箱 * 自动装箱:基本类型自动转为包装类对象 * 自动拆箱:包装类对象自动转化为基本数据类型 * * * /*缓存问题*/ /* ...

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

    Iterator(迭代器) 所有实现了Collection接口的容器都有一个iterator方法, 用来返回一个实现了Iterator接口的对象 Iterator对象称作迭代器, 用来方便的实现对容器 ...

  7. int和Integer的自动拆箱/装箱相关问题

    java中为没一种基本类型都提供相应的包装类型. byte,short,char,int,long,float,double和boolean Byte,Short,Character,Integer, ...

  8. [C#学习笔记]你真的理解拆箱装箱吗?

    学习一项新知识的时候,最好的方法就是去实践它. 前言 <CLR via C#>这本神书真的是太有意思了!没错我的前言就是这个. 装箱 首先来看下,下面这段代码 可以看到,每次循环迭代都会初 ...

  9. java 对象 拆箱装箱 编译和反编译的验证

    创建对象 package 创建对象的个数; public class main { public static void main(String[] agrs){ Check c1=new Check ...

随机推荐

  1. Hadoop集群(第13期)_HBase 常用Shell命令

    进入hbase shell console$HBASE_HOME/bin/hbase shell如果有kerberos认证,需要事先使用相应的keytab进行一下认证(使用kinit命令),认证成功之 ...

  2. 去哪网实习总结:如何配置数据库连接(JavaWeb)

    本来是以做数据挖掘的目的进去哪网的,结构却成了系统开发.. . 只是还是比較认真的做了三个月,老师非常认同我的工作态度和成果. .. 实习立即就要结束了.总结一下几点之前没有注意过的变成习惯和问题,分 ...

  3. 页面与后台传递中文乱码问题(java乱码)

    1.前台中文传递到后台乱码. 前台不须要处理, 系统一般都会默认把中文转化为ISO-8859-1类型. 仅仅需在后台接受数据是处理 Str为前台传过来的中文字符串: String inputer = ...

  4. 积跬步,聚小流------Bootstrap学习记录(1)

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...

  5. Android之应用开发基础

    Android应用开发基础 英文地址:http://developer.android.com/guide/components/fundamentals.html 本人英语水平不高,如有翻译不当请指 ...

  6. html5开发手机打电话发短信功能,html5的高级开发,html5开发大全,html手机电话短信功能具体解释

    在非常多的手机站点上,有打电话和发短信的功能,对于这些功能是怎样实现的呢.事实上不难,今天我们就用html5来实现他们. 简单的让你大开眼界.HTML5 非常easy写,但创建网页时,您常常须要反复做 ...

  7. HDU 5672 String 尺取法追赶法

    String Problem Description There is a string S.S only contain lower case English character.(10≤lengt ...

  8. 英语发音规则---W字母

    英语发音规则---W字母 一.总结 一句话总结: 1.W在单词开头发[w]? week [wiːk] n. 周,星期 win [wɪn] vt. 赢得 wake [weɪk] vi. 醒来 sweet ...

  9. hdoj--3062--party(2-sat 可行解)

    Party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  10. 39.Qt XML

    1.使用QXmlStreamReader读取XML,可以读取内存中容纳不了的特大文件,或者读取在XML文档中定制的文档的内容. xml文件(in1.xml) <?xml version=&quo ...