arrays - Multiplying a matrix by a vector of scalars in parallel C# -
i have 2 dictionaries of arrays multiplied , stored in dictionary of strings , int[]. code works
public class program { public static void main(string[] args) { int[] = {1,0,3,4,0}; int[] b = { 3, 0, 9, 10, 0}; int[] c = {2,3,3,5,0}; var ret = new dictionary<string, int[]>(); ret.add("jack", a); ret.add("jane", b); ret.add("james", c); var multipliers = new dictionary<string, int>(); multipliers.add("jack", 2); multipliers.add("jane", 3); multipliers.add("james", 5); var results = new dictionary<string, int[]>(); foreach (var item in ret) { int[] arr= new int[item.value.length]; (int = 0; < item.value.length; ++) { int d = item.value[i] * multipliers[item.key]; arr[i] = d; } results.add(item.key,arr); } } }
what efficient way this? there way in parallel?
the following code snippet way in parallel:
var results = new concurrentdictionary<string, int[]>(); parallel.foreach(ret, pair => { var srcarr = pair.value; var arr = new int[srcarr.length]; var multby = multipliers[pair.key]; (var = 0; < srcarr.length; i++) { var d = srcarr[i] * multby; arr[i] = d; } results[pair.key] = arr; });
Comments
Post a Comment