I have question about difference of following code: I will show the code first used with normal for loop.
Guesser class contructor starts mixing the character array and startBruteForce
calls the function which permutates it in the key with the appropriate length that is matched to the private password. To every new key, I setted Console.Write (key);
And results was different in each loop.
DateTime timeStarted = DateTime.Now;
Console.WriteLine("Start BruteForce - {0}", timeStarted.ToString());
for (int i = 0; i < Environment.ProcessorCount; i++)
{
Guesser guesser = new Guesser();
Thread thread = new Thread(guesser.startBruteForce);
thread.Start();
}
while (!Guesser.isMatched) ;
Console.WriteLine("Time passed: {0}s", DateTime.Now.Subtract(timeStarted).TotalSeconds);
and Parallel.For
DateTime timeStarted = DateTime.Now;
Console.WriteLine("Start BruteForce - {0}", timeStarted.ToString());
Parallel.For(0, Environment.ProcessorCount , (i) =>
{
Guesser guesser = new Guesser();
guesser.startBruteForce();
});
while (!Guesser.isMatched) ;
Console.WriteLine("Time passed: {0}s", DateTime.Now.Subtract(timeStarted).TotalSeconds);
In ParallelFor
it was something like: a,b,c,d,e,f...ab,ac,ad...
While in normal foor loop it was much more permutated for ex: b,r,1,a,k,b,ed
It look like ParrarelFor
created only one thread.
Normal for loop performs the brute-force algorithm faster than Parallel.For
loop.
My question is why this is happening.
- Another thing that interests me is why if I will narrow the number of iterations to
Environment.ProcessorCount/2
both algorithms execution time are faster than just iterted with unchangedEnvironment.ProcessorCount
.