我的计算机具有:24个内核,32 GB RAM
我已经知道了一段时间,并阅读了使用Parallel.ForEach
,但我从来没有完全了解如何准确地做到这一点。我将借此机会提供我的实时示例,该示例使用了我现在使用的单个循环,其中包含以下代码。
现在需要57秒才能执行代码。
此循环中发生了超过300万个循环/计算。
只是给出我在这里粘贴的代码测试的循环,因为没有给该功能的记忆。
是否有解决方案如何使用与计算机一样多的内核执行此代码?(我的计算机中有24个内核)
代码返回这两个列表:
List<String> minusLIST
List<String> plusLIST
public void calculationFunction(Dictionary<String, List<String>> tr1, Dictionary<String, List<ArrayList>> v1, Dictionary<String, bool> v5,
Dictionary<String, int> infoIndex, out List<String> minusLIST, out List<String> plusLIST)
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
double num1 = 0; double num2 = 0; double num3 = 0; double number1 = 0; double number2 = 0; double number3 = 0; double thenum1 = 0; double thenum2 = 0; double thenum3 = 0;
double NUM1 = 0; double NUM2 = 0; double NUM3 = 0; double NUMBER1 = 0; double NUMBER2 = 0; double NUMBER3 = 0; String str = ""; String num11 = ""; String num22 = ""; String num33 = "";
String number11 = ""; String number22 = ""; String number33 = ""; double calc1 = 0;
minusLIST = new List<String>(); plusLIST = new List<String>();
foreach (KeyValuePair<String, List<String>> entry in tr1)
{
List<String> allparams = new List<String>(entry.Value);
if (allparams.Count == 10)
{
if (v5.ContainsKey(allparams[7]) && v5.ContainsKey(allparams[8]) && v5.ContainsKey(allparams[9]))
{
if (v5[allparams[7]] && v5[allparams[8]] && v5[allparams[9]])
{
//Calculate both calc scenarios!
num1 = (double)v1[allparams[0]][infoIndex[allparams[7]]][0]; number1 = (double)v1[allparams[0]][infoIndex[allparams[7]]][1]; thenum1 = (double)v1[allparams[0]][infoIndex[allparams[7]]][2];
num2 = (double)v1[allparams[1]][infoIndex[allparams[8]]][0]; number2 = (double)v1[allparams[1]][infoIndex[allparams[8]]][1]; thenum2 = (double)v1[allparams[1]][infoIndex[allparams[8]]][2];
num3 = (double)v1[allparams[2]][infoIndex[allparams[9]]][0]; number3 = (double)v1[allparams[2]][infoIndex[allparams[9]]][1]; thenum3 = (double)v1[allparams[2]][infoIndex[allparams[9]]][2];
NUM1 = num1; NUM2 = num2; NUM3 = num3; NUMBER1 = number1; NUMBER2 = number2; NUMBER3 = number3;
if (num1 <= 0 || number1 <= 0) { NUM1 = thenum1; NUMBER1 = thenum1; }
if (num2 <= 0 || number2 <= 0) { NUM2 = thenum2; NUMBER2 = thenum2; }
if (num3 <= 0 || number3 <= 0) { NUM3 = thenum3; NUMBER3 = thenum3; }
if (NUM1 > 0 && NUM2 > 0 && NUM3 > 0 && NUMBER1 > 0 && NUMBER2 > 0 && NUMBER3 > 0)
{
str = ""; num11 = ""; num22 = ""; num33 = ""; number11 = ""; number22 = ""; number33 = "";
if (num1 > 0 && num2 > 0 && num3 > 0 && number1 > 0 && number2 > 0 && number3 > 0) { } else { str = string.Format("{0:F10}", thenum1) + " / " + string.Format("{0:F10}", thenum2) + " / " + string.Format("{0:F10}", thenum3); }
if (num1 <= 0) { num11 = "0"; num1 = thenum1; } else { num11 = string.Format("{0:F10}", num1); }
if (num2 <= 0) { num22 = "0"; num2 = thenum2; } else { num22 = string.Format("{0:F10}", num2); }
if (num3 <= 0) { num33 = "0"; num3 = thenum3; } else { num33 = string.Format("{0:F10}", num3); }
if (number1 <= 0) { number11 = "0"; number1 = thenum1; } else { number11 = string.Format("{0:F10}", number1); }
if (number2 <= 0) { number22 = "0"; number2 = thenum2; } else { number22 = string.Format("{0:F10}", number2); }
if (number3 <= 0) { number33 = "0"; number3 = thenum3; } else { number33 = string.Format("{0:F10}", number3); }
//Calculate
if (allparams[6] == "0")
{
calc1 = ((num1 * number2 * number3) - 45) / 10;
}
else
{
calc1 = (((num1 * number2) / number3) + 45) / 10;
}
//String
str = calc1 + "," + allparams[0] + " - " + allparams[1] + " - " + allparams[2] + "," +
allparams[3] + " - " + allparams[4] + " - " + allparams[5] + "," +
num11 + " / " + num22 + " / " + num33 + "," +
number11 + " / " + number22 + " / " + number33 + "," +
str + "," +
calc1 + "%";
if (calc1 > 0) { plusLIST.Add(str); }
else
{
minusLIST.Add(str);
}
}
}
}
}
}
}
我确实将并行放置在代码中。我还注意到我必须"必须?"放置一个锁定对象,因为下面的列表似乎与其他线程共享变量时似乎完全混乱。
if (calc1 > 0) { plusLIST2.Add(str); }
else
{
minusLIST2.Add(str);
}
似乎并行。
此代码运行时花费了:62秒-VS- 57秒,带一个循环。
不可能用24个内核更快地进行此操作?我知道锁定对象也会阻止代码自由运行的代码?
public void calculationFunction(Dictionary<String, List<String>> tr1, Dictionary<String, List<ArrayList>> v1, Dictionary<String, bool> v5,
Dictionary<String, int> infoIndex, out List<String> minusLIST, out List<String> plusLIST)
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
double num1 = 0; double num2 = 0; double num3 = 0; double number1 = 0; double number2 = 0; double number3 = 0; double thenum1 = 0; double thenum2 = 0; double thenum3 = 0;
double NUM1 = 0; double NUM2 = 0; double NUM3 = 0; double NUMBER1 = 0; double NUMBER2 = 0; double NUMBER3 = 0; String str = ""; String num11 = ""; String num22 = ""; String num33 = "";
String number11 = ""; String number22 = ""; String number33 = ""; double calc1 = 0;
minusLIST = new List<String>(); plusLIST = new List<String>();
List<String> minusLIST2 = new List<String>(); List<String> plusLIST2 = new List<String>(); object lockobj = new object();
Parallel.ForEach(tr1, entry =>
{
List<String> allparams = new List<String>(entry.Value);
if (allparams.Count == 10)
{
if (v5.ContainsKey(allparams[7]) && v5.ContainsKey(allparams[8]) && v5.ContainsKey(allparams[9]))
{
if (v5[allparams[7]] && v5[allparams[8]] && v5[allparams[9]])
{
//Calculate both calc scenarios!
num1 = (double)v1[allparams[0]][infoIndex[allparams[7]]][0]; number1 = (double)v1[allparams[0]][infoIndex[allparams[7]]][1]; thenum1 = (double)v1[allparams[0]][infoIndex[allparams[7]]][2];
num2 = (double)v1[allparams[1]][infoIndex[allparams[8]]][0]; number2 = (double)v1[allparams[1]][infoIndex[allparams[8]]][1]; thenum2 = (double)v1[allparams[1]][infoIndex[allparams[8]]][2];
num3 = (double)v1[allparams[2]][infoIndex[allparams[9]]][0]; number3 = (double)v1[allparams[2]][infoIndex[allparams[9]]][1]; thenum3 = (double)v1[allparams[2]][infoIndex[allparams[9]]][2];
NUM1 = num1; NUM2 = num2; NUM3 = num3; NUMBER1 = number1; NUMBER2 = number2; NUMBER3 = number3;
if (num1 <= 0 || number1 <= 0) { NUM1 = thenum1; NUMBER1 = thenum1; }
if (num2 <= 0 || number2 <= 0) { NUM2 = thenum2; NUMBER2 = thenum2; }
if (num3 <= 0 || number3 <= 0) { NUM3 = thenum3; NUMBER3 = thenum3; }
if (NUM1 > 0 && NUM2 > 0 && NUM3 > 0 && NUMBER1 > 0 && NUMBER2 > 0 && NUMBER3 > 0)
{
lock (lockobj)
{
str = ""; num11 = ""; num22 = ""; num33 = ""; number11 = ""; number22 = ""; number33 = "";
if (num1 > 0 && num2 > 0 && num3 > 0 && number1 > 0 && number2 > 0 && number3 > 0) { } else { str = string.Format("{0:F10}", thenum1) + " / " + string.Format("{0:F10}", thenum2) + " / " + string.Format("{0:F10}", thenum3); }
if (num1 <= 0) { num11 = "0"; num1 = thenum1; } else { num11 = string.Format("{0:F10}", num1); }
if (num2 <= 0) { num22 = "0"; num2 = thenum2; } else { num22 = string.Format("{0:F10}", num2); }
if (num3 <= 0) { num33 = "0"; num3 = thenum3; } else { num33 = string.Format("{0:F10}", num3); }
if (number1 <= 0) { number11 = "0"; number1 = thenum1; } else { number11 = string.Format("{0:F10}", number1); }
if (number2 <= 0) { number22 = "0"; number2 = thenum2; } else { number22 = string.Format("{0:F10}", number2); }
if (number3 <= 0) { number33 = "0"; number3 = thenum3; } else { number33 = string.Format("{0:F10}", number3); }
//Calculate
if (allparams[6] == "0")
{
calc1 = ((num1 * number2 * number3) - 45) / 10;
}
else
{
calc1 = (((num1 * number2) / number3) + 45) / 10;
}
//String
str = calc1 + "," + allparams[0] + " - " + allparams[1] + " - " + allparams[2] + "," +
allparams[3] + " - " + allparams[4] + " - " + allparams[5] + "," +
num11 + " / " + num22 + " / " + num33 + "," +
number11 + " / " + number22 + " / " + number33 + "," +
str + "," +
calc1 + "%";
if (calc1 > 0) { plusLIST2.Add(str); }
else
{
minusLIST2.Add(str);
}
}
}
}
}
}
});
plusLIST = new List<String>(plusLIST2);
minusLIST = new List<String>(minusLIST2);
}
我成功地在a:&quot'parallel.foreach(tr1,entr1,entry =&gt; quord loop中运行代码。当我确实在循环中声明所有变量时。现在的变量并没有在线程中混在一起。另外,当.ADD到2个列表时,我需要放置一个锁定对象。
现在花了12秒(Parallel.Foreach)-VS- 57秒(单个循环)因此,这是4.75次的改进。
但是,我确实有24个内核,所以我想知道为什么它不会速度至少15-20次?
为了使用尽可能多/所有内核我该怎么办?
public void calculationFunction(Dictionary<String, List<String>> tr1, Dictionary<String, List<ArrayList>> v1, Dictionary<String, bool> v5,
Dictionary<String, int> infoIndex, out List<String> minusLIST, out List<String> plusLIST)
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
minusLIST = new List<String>(); plusLIST = new List<String>();
List<String> minusLIST2 = new List<String>(); List<String> plusLIST2 = new List<String>(); object lockobj = new object();
Parallel.ForEach(tr1, entry =>
{
List<String> allparams = new List<String>(entry.Value);
if (allparams.Count == 10)
{
if (v5.ContainsKey(allparams[7]) && v5.ContainsKey(allparams[8]) && v5.ContainsKey(allparams[9]))
{
if (v5[allparams[7]] && v5[allparams[8]] && v5[allparams[9]])
{
double num1 = 0; double num2 = 0; double num3 = 0; double number1 = 0; double number2 = 0; double number3 = 0; double thenum1 = 0; double thenum2 = 0; double thenum3 = 0;
double NUM1 = 0; double NUM2 = 0; double NUM3 = 0; double NUMBER1 = 0; double NUMBER2 = 0; double NUMBER3 = 0; String str = ""; String num11 = ""; String num22 = ""; String num33 = "";
String number11 = ""; String number22 = ""; String number33 = ""; double calc1 = 0;
//Calculate both calc scenarios!
num1 = (double)v1[allparams[0]][infoIndex[allparams[7]]][0]; number1 = (double)v1[allparams[0]][infoIndex[allparams[7]]][1]; thenum1 = (double)v1[allparams[0]][infoIndex[allparams[7]]][2];
num2 = (double)v1[allparams[1]][infoIndex[allparams[8]]][0]; number2 = (double)v1[allparams[1]][infoIndex[allparams[8]]][1]; thenum2 = (double)v1[allparams[1]][infoIndex[allparams[8]]][2];
num3 = (double)v1[allparams[2]][infoIndex[allparams[9]]][0]; number3 = (double)v1[allparams[2]][infoIndex[allparams[9]]][1]; thenum3 = (double)v1[allparams[2]][infoIndex[allparams[9]]][2];
NUM1 = num1; NUM2 = num2; NUM3 = num3; NUMBER1 = number1; NUMBER2 = number2; NUMBER3 = number3;
if (num1 <= 0 || number1 <= 0) { NUM1 = thenum1; NUMBER1 = thenum1; }
if (num2 <= 0 || number2 <= 0) { NUM2 = thenum2; NUMBER2 = thenum2; }
if (num3 <= 0 || number3 <= 0) { NUM3 = thenum3; NUMBER3 = thenum3; }
if (NUM1 > 0 && NUM2 > 0 && NUM3 > 0 && NUMBER1 > 0 && NUMBER2 > 0 && NUMBER3 > 0)
{
str = ""; num11 = ""; num22 = ""; num33 = ""; number11 = ""; number22 = ""; number33 = "";
if (num1 > 0 && num2 > 0 && num3 > 0 && number1 > 0 && number2 > 0 && number3 > 0) { } else { str = string.Format("{0:F10}", thenum1) + " / " + string.Format("{0:F10}", thenum2) + " / " + string.Format("{0:F10}", thenum3); }
if (num1 <= 0) { num11 = "0"; num1 = thenum1; } else { num11 = string.Format("{0:F10}", num1); }
if (num2 <= 0) { num22 = "0"; num2 = thenum2; } else { num22 = string.Format("{0:F10}", num2); }
if (num3 <= 0) { num33 = "0"; num3 = thenum3; } else { num33 = string.Format("{0:F10}", num3); }
if (number1 <= 0) { number11 = "0"; number1 = thenum1; } else { number11 = string.Format("{0:F10}", number1); }
if (number2 <= 0) { number22 = "0"; number2 = thenum2; } else { number22 = string.Format("{0:F10}", number2); }
if (number3 <= 0) { number33 = "0"; number3 = thenum3; } else { number33 = string.Format("{0:F10}", number3); }
//Calculate
if (allparams[6] == "0")
{
calc1 = ((num1 * number2 * number3) - 45) / 10;
}
else
{
calc1 = (((num1 * number2) / number3) + 45) / 10;
}
//String
str = calc1 + "," + allparams[0] + " - " + allparams[1] + " - " + allparams[2] + "," +
allparams[3] + " - " + allparams[4] + " - " + allparams[5] + "," +
num11 + " / " + num22 + " / " + num33 + "," +
number11 + " / " + number22 + " / " + number33 + "," +
str + "," +
calc1 + "%";
if (calc1 > 0)
{
lock (lockobj) { plusLIST2.Add(str); }
}
else
{
lock (lockobj) { minusLIST2.Add(str); }
}
}
}
}
}
});
plusLIST = new List<String>(plusLIST2);
minusLIST = new List<String>(minusLIST2);
}