I'm toying with Azure VMs equipped with hyper-threading CPUs. F2_v2 size VM is claimed to have a single core with hyper-threading and so two "virtual" CPUs (2.7 GHz Intel Xeon® Platinum 8168 (SkyLake) is claimed to be used there). I use code from this answer to craft a CPU-intensive load.
static void doWork()
{
var start = DateTime.UtcNow;
var value = FindPrimeNumber(1024 * 1024);
var end = DateTime.UtcNow;
Console.WriteLine(String.Format("Value {0} found in {1} seconds",
value, (end-start).TotalSeconds));
}
First I just call that function from inside Main()
:
static void Main(string[] args)
{
doWork();
}
and the time reported is around 32 seconds. I then run two processes from that executable (using two consoles and being fast) - each then slows down and time reported gets to around 64 seconds - exactly as expected because well, there's a single real CPU and it cannot run two processes in parallel.
Then I try to run two threads in parallel using Parallel.Invoke()
:
static void Main(string[] args)
{
Parallel.Invoke(() => doWork(), () => doWork());
}
(yes, two threads concurrently obtain time and concurrently write to the console afterwards but I expect that this shouldn't affects the results much) and the time reported becomes about 60 seconds - slightly faster than with two processes. This test is done using Windows command prompt, not by invoking under debugger.
I expected that with hyper-threading I could run two threads concurrently and have twice as much computational work completed in the same time - something close to 95% speedup. Looks like it isn't the case.
What am I doing wrong? How can I do it right?