I am trying to verify the threading of a multi-threaded program in a virtual machine with multiple cores. I wrote C # code for it:
class Program
{
public static int fib(int n)
{
if (n < 2)
return n;
return fib(n-1)+fib(n-2);
}
public static void execution(object n)
{
int STEP = 40;
var start = DateTime.Now;
int value = fib(STEP);
var end = DateTime.Now;
Console.WriteLine(string.Format("threads: {0}, time : {1}, start: {2}, end: {3}", n, end.Subtract(start).TotalSeconds,
start, end));
}
static void Main(string[] args)
{
int[] threads = {1, 2, 4, 8, 16};
for(int j=0; j<5; ++j)
{
for (int i = 0; i < threads[j]; ++i)
{
var thread = new Thread(Program.execution);
thread.Start(threads[j]);
}
Thread.Sleep(60000);
}
}
and this is the result that I got
topics: 1, time: 4.2177734, start: 02/08/2012 7:30:13, end: 2/8/2014 7:30:18 PM
topics: 2, time: 4.1015625, start: 02/08/2012 19:31:13, end: 2/8/2014 7:31:17 PM
topics: 2, time: 4.2441407, start: 02/08/2012 19:31:13, end: 2/8/2014 7:31:18 PM
topics: 4, time: 2.0351562, start: 02/08/2012 19:32:13, end: 2/8/2014 7:32:15
topics: 4, time: 2.0527343, start: 08/02/2012 19:32:13, end: 2/8/2014 7:32:15 PM
threads: 4, time: 2.0869141, start: 02/08/2012 19:32:13, end: 2/8/2014 7:32:15 PM
topics: 4, time: 2.0898437, start: 2/8/2014 7:32:13, end: 2/8/2014 7:32:15 PM
: 8, : 3.34375, : 02.08.2012 19:33:13, : 2/8/2014 7:33:17 PM
: 8, : 3.381836, : 02.08.2014 19:33:13, : 2/8/2014 7:33:17 PM
threads: 8, : 3.3066406, : 08.02.2012 19:33:14, : 2/8/2014 7:33:17 PM
threads: 8, : 3.2451172, : 08.02.2012 19:33:14, : 2/8/2014 7:33:17 PM
: 8, : 3.4560547, : 02.08.2012 19:33:13, : 2/8/2014 7:33:17 PM
: 8, : 3.5029296, : 08.02.2012 19:33:13, : 2/8/2014 7:33:17 PM
threads: 8, : 3.2841796, : 08.02.2012 19:33:14, : 02.08.2012 7:33:17 PM
: 8, : 3.4160157, : 08.02.2012 19:33:14, : 2/8/2014 7:33:17 PM
: 16, : 5.9921875, : 08.02.2012 19:34:14, : 2/8/2014 7:34:20 PM
: 16, : 6.4404297, : 08.02.2012 19:34:14, : 2/8/2014 7:34:20 PM
: 16, : 5.3896484, : 2/8/2014 7:34:15 PM, : 2/8/2014 7:34:20 PM
: 16, : 5.9658203, : 08.02.2012 19:34:14, : 2/8/2014 7:34:20 PM
: 16, : 5.9873047, : 08.02.2012 19:34:14, : 2/8/2014 7:34:20 PM
: 16, : 6.2226563, : 08.02.2012 19:34:14, : 2/8/2014 7:34:20 PM
: 16, : 6.1552735, : 08.02.2012 19:34:14, : 2/8/2014 7:34:20 PM
: 16, : 6.5576172, : 08.02.2012 19:34:14, : 2/8/2014 7:34:20 PM
: 16, : 6.5273437, : 08.02.2012 19:34:14, : 2/8/2014 7:34:20 PM
: 16, : 6.2529297, : 08.02.2012 19:34:14, : 2/8/2014 7:34:20 PM
: 16, : 6.2958984, : 08.02.2012 19:34:14, : 2/8/2014 7:34:20 PM
: 16, : 5.8544922, : 2/8/2014 7:34:15 PM, : 2/8/2014 7:34:20 PM
: 16, : 6.3886719, : 08.02.2012 19:34:14, : 2/8/2014 7:34:20 PM
: 16, : 5.7089844, : 2/8/2014 7:34:15 PM, : 2/8/2014 7:34:20 PM
: 16, : 6.7207031, : 08.02.2012 19:34:14, : 2/8/2014 7:34:20 PM
: 16, : 6.0742188, : 08.02.2012 19:34:14, : 2/8/2014 7:34:21 PM
, 4- Windows 7.
, , 4 , , , 1 2 , .
- ?