Why does multithreading give a worse performence here?
I enumerate through a bitarray setting every second bit to false.
Now I'd like to speed this up by splitting it up into two threads.. for
some weird reason though, the time per Thread to do the HALF amount of
work takes 64% MORE time, and I wonder why's that?
Could this be due to some kind of CPU caching effect? How do I do this
properly?
I have tried 8 threads too previously with lambda expressions but it was
always around ~1400 ms, however in single threading I constandly got 850
ms. Also when I let a single thread do all the work, it took me 830 ms. I
just don't understand, anyone knowing the cause for that here?
Code:
class Program
{
static int count = 0x10000000;
static int half = count / 2;
static BitArray bitArray = new BitArray(count);
static unsafe void Main(string[] args)
{
Stopwatch sw = Stopwatch.StartNew();
#if SINGLE
for (int i = 0; i < bitArray.Count; i += 2)
bitArray.Set(i, true);
#else
Thread thread1 = new Thread(Thread1);
Thread thread2 = new Thread(Thread2);
thread1.Start();
thread2.Start();
thread1.Join();
thread2.Join();
No comments:
Post a Comment