网络编程(2)
程序员文章站
2024-03-24 19:46:22
...
08 并行
并行串行计算比较
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace test07
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private async void bt_Click(object sender, RoutedEventArgs e)
{
long[] t1 = await Task.Run(() => Multiply(200, 18, 27));
tb.Text = string.Format("测试1(矩阵1:200x18,矩阵2:18x27),非并行用时{0}毫秒,并行用时{1}毫秒\n", t1[0], t1[1]);
long[] t2 = await Task.Run(() => Multiply(2000, 180, 270));
tb.Text += string.Format("测试1(矩阵1:2000x180,矩阵2:180x270),非并行用时{0}毫秒,并行用时{1}毫秒\n", t2[0], t2[1]);
long[] t3 = await Task.Run(() => Multiply(2000, 200, 300));
tb.Text += string.Format("测试1(矩阵1:2000x200,矩阵2:200x300),非并行用时{0}毫秒,并行用时{1}毫秒\n", t3[0], t3[1]);
}
/// <summary>
/// 传入行数和列数,初始化一个矩阵
/// </summary>
/// <param name="rows"></param>
/// <param name="cols"></param>
/// <returns></returns>
public static int[,] InitializeMatrix(int rows, int cols)
{
int[,] matrix = new int[rows, cols];
Random r = new Random();
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
matrix[i, j] = r.Next(100);
}
}
return matrix;
}
/// <summary>
/// 普通方法计算矩阵乘法
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
public static void CommonCompute(int[,] a,int[,] b)
{
int arows = a.GetLength(0);
int acols = a.GetLength(1);
int bcols = b.GetLength(1);
int[,] result = new int[arows, bcols];
for(int i = 0; i < arows; i++)
{
for(int j = 0; j < bcols; j++)
{
int temp = 0;
for (int k = 0; k < acols; k++)
{
temp += a[i, k] * b[k, j];
}
result[i, j] = temp;
}
}
}
/// <summary>
/// 并行方法计算矩阵乘法
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
public static void ParallelCompute(int[,] a, int[,] b)
{
int arows = a.GetLength(0);
int acols = a.GetLength(1);
int bcols = b.GetLength(1);
int[,] result = new int[arows, bcols];
Action<int> action = i =>
{
for (int j = 0; j < bcols; j++)
{
int temp = 0;
for (int k = 0; k < acols; k++)
{
temp += a[i, k] * b[k, j];
}
result[i, j] = temp;
}
};
//外循环并行执行
Parallel.For(0, arows, action);
}
/// <summary>
/// 计算矩阵乘法
/// </summary>
/// <param name="rows"></param>
/// <param name="cols"></param>
/// <param name="cols2"></param>
/// <returns></returns>
public static long[] Multiply(int rows,int cols,int cols2)
{
//初始化矩阵
int[,] a = InitializeMatrix(rows, cols);
int[,] b = InitializeMatrix(cols, cols2);
Stopwatch watch = new Stopwatch();
long[] timeElapsed = new long[2];
//串行
watch.Start();
CommonCompute(a, b);
watch.Stop();
timeElapsed[0] = watch.ElapsedMilliseconds;
//并行
watch.Restart();
ParallelCompute(a, b);
watch.Stop();
timeElapsed[1] = watch.ElapsedMilliseconds;
return timeElapsed;
}
}
}
推荐阅读
-
oracle自定义函数 博客分类: oracle oracle高级编程
-
网络编程(2)
-
Windows下的命令行工具2-ping
-
24 -Oracle学习(2)- 两种函数
-
ElasticSearch及Kibana的X-pack破解2 ElasticSearchKibanaX-pack破解
-
分享一个无线网用于手机端接受 博客分类: network 分享无线网网络
-
说明SOA监管(SOA Governance)实例(收录备查) 博客分类: 网络Framework soa企业应用
-
JAVA UDP打洞必备知识点---NAT 博客分类: Java Java网络应用网络协议应用服务器出版
-
2,如何在本地使用git与git commit -v
-
m2eclipse: Eclipse is running in a JRE, but a JDK is required 博客分类: Java EclipseJDKmavenWindowsC