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