WPF中多線程統計拆箱裝箱和泛型的運行效率
阿新 • • 發佈:2018-02-18
eset 主線程 ted fonts reset 通知 ack foreach -c
MainWindow.xaml.cs
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中多線程統計拆箱裝箱和泛型的運行效率