I have a huge amount of data(permutation of the characters a-z,0-9) that needs to be processed by a method(ReturnId). But this takes a long time, so I would like to split the input and feed into the same method multiple times.
I guess I should work with threads but I don´t know how the separate the input properly.
static void Main(string[] args)
{
string characters = "abcdefghijklmnopqrstuvwxyz0123456789";
List<string> combinations = new List<string>();
List<int> Id = new List<int>();
combinations = Permutations(characters, 3); //Method that creats all the permutations up to 3 characters
foreach (var item in combinations)
{
Id.Add(ReturnId(item)); // Methode that takes the string and search based on that string on a website for an id.
}
}
}
答案 0 :(得分:0)
使用Parallel.ForEach
根据环境系统在多个线程上安排工作。系统上的处理器越多,并行方法运行的速度就越快。 :
Parallel.ForEach(combinations, (item) =>
{
Id.Add(ReturnId(item));
});
您还可以使用ParallelOptions
定义最大并行度:
Parallel.ForEach(combinations, new ParallelOptions { MaxDegreeOfParallelism = 10 }, (item) =>
{
Id.Add(ReturnId(item));
});
有关更多信息,请阅读here。
答案 1 :(得分:0)
i'm guessing you want something like
public IEnumerable<string> Permutations(IEnumerable<string> characters ,int length,int current = 1)
{
return current >= length
? characters
: from c in characters.AsParallel()
from e in Permutations(characters, length, current+1)
select c + e;
}
note this is not tested code and is provided as a guideline only