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. Eclipse设置jdk相关

    2.window->preferences->java->Compiler->设置右侧的Compiler compliance level 3.window->prefe ...

  2. mycat 连续分片 -&gt; 自己定义数字范围分片

    1,自己定义数字范围分片 自己定义数字范围分片,提前规划好分片字段某个范围属于哪个分片,比方说将第一个500W的数据分片在第一个节点上面.第二个500W的数据分片在第二个节点上,依次类推 2,加入配置 ...

  3. 组件的使用(三)AutoCompleteTextView的使用

    AutoCompleteTextView经常使用的属性: android:completionHint 下拉列表以下的说明性文字 android:completionThreshold 弹出下来列表的 ...

  4. xcode对照两个分支中同一个文件

    对于同一个项目的两个分支,由于两个分支可能各自都做了一些改动.所以通过Source Control中的History...功能是无法查看的.例如以下图: 这个时候.我们须要用到xcode的另外一个功能 ...

  5. HDU 4686

    再不能直视这道题,换INT64就过了....... 同样可以使用矩阵的方法.构造1*5的 D[N],a[n],b[n],a[n]*b[n],1 接着你应该就会了. #include <iostr ...

  6. JavaScript——BOM(浏览器对象模型),时间间隔和暂停

    BOM(浏览器对象模型):能够对浏览器的窗体进行訪问和操作 1.主要的BOM体系: window------------document-------------------------------- ...

  7. Android::开机自启动C程序【转】

    本文转载自:http://blog.csdn.net/Kaiwii/article/details/7681736 之前一篇博文介绍了shell脚本文件的开机启动,地址是http://blog.chi ...

  8. 15.Linux的文件结构

    linux的文件结构和windows不同,没有分区,是树形的结构: /etc:存放配置文件 /lib:编译程序需要的函数库 /usr:包含所有其他内容,比如内核在/usr/src中,/usr/bin存 ...

  9. SQL Server的复合索引学习【转载】

      概要什么是单一索引,什么又是复合索引呢? 何时新建复合索引,复合索引又需要注意些什么呢?本篇文章主要是对网上一些讨论的总结. 一.概念 单一索引是指索引列为一列的情况,即新建索引的语句只实施在一列 ...

  10. SQL like查询条件中的通配符处理

    1. SQL like对时间查询的处理方法 SQL数据表中有savetime(smalldatetime类型)字段,表中有两条记录,savetime值为:2005-3-8 12:12:00和2005- ...