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 ...
随机推荐
- spring datasource和mybatis的datasource来源在哪里
配置一个数据源 spring在第三方依赖包中包含了两个数据源的实现类包,其一是Apache的DBCP,其二是 C3P0.可以在Spring配置文件中利用这两者中任何一个配置数据源. 配置一个 ...
- 升级后开机就提示“android.process.acore”停止执行 --分析 解决方式
OTA升级的,升级引发的全部问题都是能够解释的,有的能解决,有的不能解决. 一个项目报了这个问题. 升级后开机就提示"android.process.acore"停止执行 抓取 a ...
- 朴素的UNIX之-调度器细节
0.多进程调度的本质 我们都知道UNIX上有一个著名的nice调用.何谓nice,当然是"好"了.常规的想法是nice值越大越好,实际上,nice值越好,自己的优先级越低.那么为何 ...
- kaggle 中使用ipython
# pandas import pandas as pd from pandas import Series,DataFrame # numpy, matplotlib, seaborn import ...
- shp系列(一)——利用C++进行shp文件的读(打开)与写(创建)开言
博客背景和目的 最近在用C++写一个底层的东西,需要读取和创建shp文件.虽然接触shp文件已经几年了,但是对于shp文件内到底包含什么东西一直是一知半解.以前使用shp文件都是利用软件(如ArcGI ...
- POJ 3150 循环矩阵的应用
思路: 首先 先普及一个性质: 循环矩阵*循环矩阵=循环矩阵 由于此题是距离小于d的都加上一个数. 那么 构造矩阵的时候 我们发现 诶呦 这是个循环矩阵 看看数据范围 n^2log(k)可以过. 那就 ...
- linux的chmod,chown命令 详解
指令名称 : chmod 使用权限 : 所有使用者 使用方式 : chmod [-cfvR] [--help] [--version] mode file... 说明 : Linux/Unix 的档案 ...
- Kylin基础教程(一)
一.Kylin介绍 1.1 现状 Hadoop于2006年初步实现,改变了企业级的大数据存储(基于HDFS)和批处理(主要基于MR)问题,10几年过去了,数据量随着互联网的发展井喷式增长,如何高速.低 ...
- P2216 [HAOI2007]理想的正方形(二维RMQ)
题目描述 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 输入输出格式 输入格式: 第一行为3个整数,分别表示a,b,n的值 第二行至 ...
- Simula-Virtual function
Simula is the name of two simulation programming languages, Simula I and Simula 67, developed in the ...