首页 > 学院 > 开发设计 > 正文

大矩阵乘法时间开销

2019-11-06 06:24:47
字体:
来源:转载
供稿:网友

利用随机数初始化数组,并计算不同量数据所消耗的时间

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace BigArray{ class PRogram { static void Main(string[] args) { DateTime d1 = System.DateTime.Now; const long M = 500; const long N = 500; long [,] A = new long [500 , 500]; long[,] B = new long[500, 500]; var listA = new List<List<long>> (); var listB = new List<List<long>>(); var listC = new List<List<long>>(); Random random = new Random(); var itemA = new List<long>(); var itemB = new List<long>(); var itemC = new List<long>(); for (long i = 0; i < M; i++) { for (long j = 0; j < N; j++) { int random_x = random.Next(0, 10); A[i, j] = random_x; int random_y = random.Next(0, 10); B[i, j] = random_y; // itemC.Add(random_y*random_x ); } /* listA.Add(itemA); listB.Add(itemB); listC.Add(itemC); */ } Multiply multiply = new Multiply(); long [,]MM= multiply.MatrixMultiply (A, B); DateTime d2 = System.DateTime.Now; TimeSpan ts = d2.Subtract(d1); Console.WriteLine("program'time_spend is:{0}ms", ts.TotalMilliseconds); Console.ReadLine(); } public class Multiply { public long[,] MatrixMultiply(long[,] A, long[,] B) { int rowA = A.GetLength(0); int colA = A.GetLength(1); int rowB = B.GetLength(0); int colB = B.GetLength(1); long[,] C = new long[rowA, colB]; try { for (long i = 0; i < rowA; i++) { for (long j = 0; j < colB; j++) { C[i, j] = 0; for (long k = 0; k < colA; k++) { C[i, j] = A[i, k] * B[k, j]; } } } return C; } catch { System.Exception exp = new Exception("前者矩阵列数不等于后者矩阵行数,不能相乘!"); throw exp; } } } }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表