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 ...
随机推荐
- crm高速开发之Entity
我们在后台代码里面操作Entity的时候,基本上是这样写的: /* 创建者:菜刀居士的博客 * 创建日期:2014年07月5号 */ namespace Net.CRM.Entity { ...
- hdoj 2795 Billboard 【线段树 单点更新 + 维护区间最大值】
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- Qt on Android:资源文件系统qrc与assets
使用 Qt 为 Android 开发应用时,有时我们的应用会携带一些资源文件,如 png . jpg 等,也可能有一些配置文件,如 xml 等.这些文件放在哪里呢? 有两种方式: qrc assets ...
- Android菜鸟笔记- 获取未安装的APK图标、版本号、包名、名称、是否安装、安装、打开
周末闲来无事,把Android的基础知识拿出来复习复习,今天主题是<获取未安装的APK图标.版本号.包名.名称.是否安装.跳转安装.打开> 一.获取APK图标 通常读取APK的图标能够用, ...
- R语言基础-数组和列表
数组(array) 一维数据是向量,二维数据是矩阵,数组是向量和矩阵的直接推广,是由三维或三维以上的数据构成的. 数组函数是array(),语法是:array(dadta, dim),当中data必须 ...
- angularjs1-8,cacheFactory,sce
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...
- 0x01 位运算
都比较基础吧. 知识点 1.快速幂和快速乘(这里有一个用long double舍弃精度的做法,但是感觉既不稳又没用) 2.懒人写边目录的时候的k^1 3.lowbit,得到的是低到高第一个1的位.求一 ...
- DB-MySQL:MySQL 正则表达式
ylbtech-DB-MySQL:MySQL 正则表达式 1.返回顶部 1. MySQL 正则表达式 在前面的章节我们已经了解到MySQL可以通过 LIKE ...% 来进行模糊匹配. MySQL 同 ...
- 4. Median of Two Sorted Arrays[H]两个有序数组的中位数
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the midian of the ...
- 取消overflow-scroll的滚动条
通常情况下设置完overflow:scroll之后,就会在页面中出现滚动条,下边的方法可以取消掉此滚动条: container为当前设置overflow:scroll的元素 1.使用以下CSS可以隐藏 ...