Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does multithreading give a worse performance here?
    primarykey
    data
    text
    <p>I enumerate through a bitarray setting every second bit to false.</p> <p>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 <strong>HALF</strong> amount of work takes 64% <strong>MORE</strong> time, and I wonder why's that? </p> <p>Could this be due to some kind of CPU caching effect? How do I do this properly?</p> <p>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?</p> <p><em>Code:</em></p> <pre><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 &lt; 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(); #endif sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); Console.ReadLine(); } static void Thread1() { Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; i &lt; half; i += 2) bitArray.Set(i, true); sw.Stop(); Console.WriteLine("Thread1: {0}", sw.ElapsedMilliseconds); } static void Thread2() { Stopwatch sw = Stopwatch.StartNew(); for (int i = half; i &lt; count; i += 2) bitArray.Set(i, true); sw.Stop(); Console.WriteLine("Thread2: {0}", sw.ElapsedMilliseconds); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload