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);
}
}
}
}
WPF中多线程统计拆箱装箱和泛型的运行效率的更多相关文章
- Java中的自动拆箱装箱(Autoboxing&Unboxing)
一.基本类型打包器 1.基本类型:long.int.double.float.boolean 2.类类型:Long.Integer.Double.Float.Boolean 区别:基本类型效率更高,类 ...
- 如何理解Java中的自动拆箱和自动装箱?
小伟刚毕业时面的第一家公司就被面试官给问住了... 如何理解Java中的自动拆箱和自动装箱? 自动拆箱?自动装箱?什么鬼,听都没听过啊,这...这..知识盲区... 回到家后小伟赶紧查资料,我透,这不 ...
- Java包装类及其拆箱装箱
Java包装类,Wrapper~由于在java中,数据类型总共可分为两大种,基本数据类型(值类型)和类类型(引用数据类型).基本类型的数据不是对象,所以对于要将数据类型作为对象来使用的情况,java提 ...
- Java 从Character和char的区别来学习自动拆箱装箱
本文结构 1.Character和char 的区别: 2.自动拆箱装箱 1.Character和char 的区别: Character是类,char基本数据类型. 在java中有三个类负责对字符的操作 ...
- 关于Java自动拆箱装箱中的缓存问题
package cn.zhang.test; /** * 测试自动装箱拆箱 * 自动装箱:基本类型自动转为包装类对象 * 自动拆箱:包装类对象自动转化为基本数据类型 * * * /*缓存问题*/ /* ...
- Java之集合初探(二)Iterator(迭代器),collections,打包/解包(装箱拆箱),泛型(Generic),comparable接口
Iterator(迭代器) 所有实现了Collection接口的容器都有一个iterator方法, 用来返回一个实现了Iterator接口的对象 Iterator对象称作迭代器, 用来方便的实现对容器 ...
- int和Integer的自动拆箱/装箱相关问题
java中为没一种基本类型都提供相应的包装类型. byte,short,char,int,long,float,double和boolean Byte,Short,Character,Integer, ...
- [C#学习笔记]你真的理解拆箱装箱吗?
学习一项新知识的时候,最好的方法就是去实践它. 前言 <CLR via C#>这本神书真的是太有意思了!没错我的前言就是这个. 装箱 首先来看下,下面这段代码 可以看到,每次循环迭代都会初 ...
- java 对象 拆箱装箱 编译和反编译的验证
创建对象 package 创建对象的个数; public class main { public static void main(String[] agrs){ Check c1=new Check ...
随机推荐
- Nginx 做系统的前端反向proxy
Nginx是一款很优秀的基于event的webserver.吞吐量大.占用资源少,只是文档就很让人郁闷了,免费的Nginx和收费的Nginx+的文档共用一份,配置完之后才发现免费的Nginx启动某些命 ...
- jenkins启动失败
一:http://localhost:8080已经可以进了 二:修改端口 只是因为8080端口已经有程序占用了,需要修改端口号,但是在jenkins.xml文件中修改端口号,并不起作用. 解决方法是: ...
- 初识MVC之建项
MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,用一种业务逻辑.数据.界面显示分离的方法组织代码,将业务逻辑聚集到 ...
- Directx Matrix.PerspectiveFovLH Matrix.PerspectiveFovRH的理解
该函数一个四个参数public static Matrix PerspectiveFovLH ( float fieldOfViewY, float aspectRatio, float znearP ...
- 英语发音规则---Q字母
英语发音规则---Q字母 一.总结 一句话总结: 1.Q/que发[k]音? Iraq [ɪ'rɑ:k] n. 伊拉克 cheque [tʃek] n. 支票 2.Qu-发[kw]? quality ...
- 杂项:ExtJS
ylbtech-杂项:ExtJS extjs是一种软件.自动生成行号,支持checkbox全选,动态选择显示哪些列,支持本地以及远程分页,可以对单元格按照自己的想法进行渲染,这些也算可以想到的功能. ...
- 常见问题处理之Emoji
所谓Emoji就是一种在Unicode位于\u1F601-\u1F64F区段的字符.这个显然超过了目前常用的UTF-8字符集的编码范围\u0000-\uFFFF.Emoji表情随着IOS的普及和微信的 ...
- BZOJ 2427 /HAOI 2010 软件安装 tarjan缩点+树形DP
终于是道中文题了.... 当时考试的时候就考的这道题.... 果断GG. 思路: 因为有可能存在依赖环,所以呢 先要tarjan一遍 来缩点. 随后就进行一遍树形DP就好了.. x表示当前的节点.j表 ...
- Bluefish Editor - 蓝鱼编辑器 for Web Designer
Bluefish is a GTK+ HTML editor for the experienced web designer. Its features include nice wizards f ...
- Fildder 4接口测试工具Post请求方式