From R.A.M's blog, I've decided to test my 64-bit Quad-Core machine to encode some videos using Expression Encoder SDK.
Note that if you use Encoder SDK in 64-bit machines, you will get error "Could not load file or assembly 'Microsoft.Expression.Encoder"
This is how you solve it, go to your Build Properties and change the Platform to x86:
So here are the results of encoding in 1,2, and 4 cores (I didn't test for 3 cores since I haven't heard of a Tri-Core CPU), but using my code below, you can certainly run it on 3 cores (weirdo :P)
So to conclude, the stats are:
Video File Size = 4MB
1 Core Encoding = 39 seconds
2 Core Encoding = 24 seconds
4 Core Encoding = 17 seconds
So YES, MORE CORE MEANS FASTER ENCODING (my point of view, and Intel Marketing Guys will jump ecstatic in agreement).
And here's the code to throttle the application to run under X amount of Cores. Note that this code WILL ONLY WORK ON SINGLE-THREADED APPLICATION. For multi-threaded apps, I leave that to some future (hopefully I have time) post.
I used the Win32 Timer to measure the running time, you can see ZeddyLabs.TimerAkurat from this post.
class Program
{
[Flags]
public enum Cores : int
{
Core1 = 1,
Core2 = 2,
Core3 = 4,
Core4 = 8
}
static Dictionary<int, Cores> GetCoresTable()
{
// I don't like too many IF-THEN-ELSE
// hence the use of lookup table
var table = new Dictionary<int, Cores>();
table[1] = Cores.Core1;
table[2] = table[1] | Cores.Core2;
table[3] = table[2] | Cores.Core3;
table[4] = table[3] | Cores.Core4;
return table;
}
static int GetCoresCount(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("Please enter ONE numeric argument");
Console.WriteLine("Usage: EncodeMultiCore <num>");
Environment.Exit(-1);
}
int numValue;
if (!int.TryParse(args[0], out numValue))
{
Console.WriteLine("Numeric argument cannot be parsed to integer");
Environment.Exit(-1);
}
return numValue;
}
static void TestEncode()
{
// From Risman Adnan's blog
const string video =
@"C:\Users\Public\Videos\Sample Videos\Bear.wmv";
var mediaItem = new MediaItem(video);
// Create job & add the media item to it
var job = new Job();
job.MediaItems.Add(mediaItem);
job.EncodeProgress +=
((sender, e) => Console.WriteLine(e.Progress));
job.OutputDirectory = @"C:\temp";
job.Encode();
}
static void Main(string[] args)
{
int cores = GetCoresCount(args);
Dictionary<int, Cores> table = GetCoresTable();
int coresToRun = (int)table[cores];
Process.GetCurrentProcess()
.ProcessorAffinity = (IntPtr) coresToRun;
var timer = new ZeddyLabs.TimerAkurat();
timer.Mulai();
TestEncode();
timer.Berhenti();
Console.WriteLine("Dengan {0} core, encode memakan waktu {1} milidetik",
cores, timer.HasilDlmMilliDetik);
}
}
Or you can download the codes from my SkyDrive below: