Skip to content

Instantly share code, notes, and snippets.

@IJzerbaard
Created October 24, 2018 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IJzerbaard/db056edd49b70f8bf06bcba5a1921274 to your computer and use it in GitHub Desktop.
Save IJzerbaard/db056edd49b70f8bf06bcba5a1921274 to your computer and use it in GitHub Desktop.
Parallel.For test
static void Main(string[] args)
{
for (int j = 0; j < 10; j++)
{
bool par = (j & 1) == 1;
var sw = Stopwatch.StartNew();
if (par)
{
Parallel.For(0, 4, test);
}
else
{
test(0);
}
sw.Stop();
Console.WriteLine("{0}: {1}", par ? "par" : "seq", sw.ElapsedTicks);
}
}
static volatile int r;
static void test(int x)
{
int s = 0;
for (int i = 0; i < 1000000000; i++)
{
s += x + i;
}
r = s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment