我正试图理解我最近在项目中遇到的一个问题。我使用Aurigma库来调整图像大小。它在单线程模式下使用,在计算过程中只产生一个线程。最近我决定转向ImageMagick项目,因为它是免费和开源的。我已经建立了IM在单线程模式,并开始测试。起初,我想在不中断的情况下比较它们的性能,因此我创建了一个对进程及其线程具有高优先级的测试。另外,我将affinity设置为一个核心。我比Aurigma快了25%。但是我添加的线程比IM少对Aurigma更有优势。
我的项目是一个启动大约7-10个子进程的windows服务。每个进程有2个线程来处理图像。当我将我的测试作为两个不同的进程运行时,每个进程有2个线程,我注意到IM的工作比Aurigma差5%左右。
也许我的问题不是很详细,但是这个范围对我来说有点新,我很高兴得到进一步调查的方向。为什么一个程序在一个进程的一个线程上运行会更快,而在多个进程中同时运行会更差呢?的例子,
Au: 8个进程x 2Th(每个线程20个任务)= 245秒320个任务
IM: 8个进程x 2Th(每个线程20个任务)= 280秒320个任务
Au: 4个进程x 2Th(每个线程20个任务)= 121秒160个任务
IM: 4个进程x 2Th(每个线程20个任务)= 141秒160个任务
我们可以看到,如果我们有多个进程,Au工作得更好,但在单进程模式下:Au处理一个任务为2,2秒,IM为1,4秒,IM的总和时间更好
private static void ThreadRunner(
Action testFunc,
int repeatCount,
int threadCount
)
{
WaitHandle[] handles = new WaitHandle[threadCount];
var stopwatch = new Stopwatch();
// warmup
stopwatch.Start();
for (int h = 0; h < threadCount; h++)
{
var handle = new ManualResetEvent(false);
handles[h] = handle;
var thread = new Thread(() =>
{
Runner(testFunc, repeatCount);
handle.Set();
});
thread.Name = "Thread id" + h;
thread.IsBackground = true;
thread.Priority = ThreadPriority.Normal;
thread.Start();
}
WaitHandle.WaitAll(handles);
stopwatch.Stop();
Console.WriteLine("All Threads Total time taken " + stopwatch.ElapsedMilliseconds);
}
private static void Runner(
Action testFunc,
int count
)
{
//Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(2); // Use only the second core
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.BelowNormal;
Process.GetCurrentProcess().PriorityBoostEnabled = false;
Thread.CurrentThread.Priority = ThreadPriority.Normal;
var stopwatch = new Stopwatch();
// warmup
stopwatch.Start();
while(stopwatch.ElapsedMilliseconds < 10000)
testFunc();
stopwatch.Stop();
long elmsec = 0;
for (int i = 0; i < count; i++)
{
stopwatch.Reset();
stopwatch.Start();
testFunc();
stopwatch.Stop();
elmsec += stopwatch.ElapsedMilliseconds;
Console.WriteLine("Ticks: " + stopwatch.ElapsedTicks +
" mS: " + stopwatch.ElapsedMilliseconds + " Thread name: " + Thread.CurrentThread.Name);
}
Console.WriteLine("Total time taken " + elmsec + " Thread name: " + Thread.CurrentThread.Name);
}
/// <summary>
/// Entry point
/// </summary>
/// <param name="args"></param>
private static void Main(string[] args)
{
var files = GetFiles(args.FirstOrDefault());
if (!files.Any())
{
Console.WriteLine("Source files were not found.");
goto End;
}
//// Run tests
Console.WriteLine("ImageMagick run... Resize");
Runner(() => PerformanceTests.RunResizeImageMagickTest(files[0]), 20);
Console.WriteLine("Aurigma run... Resize");
Runner(() => PerformanceTests.RunResizeAurigmaTest(files[0]), 20);
Console.WriteLine("ImageMagick run... multi Resize");
ThreadRunner(() => PerformanceTests.RunResizeImageMagickTest(files[0]), 20, 2);
Console.WriteLine("Aurigma run... multi Resize");
ThreadRunner(() => PerformanceTests.RunResizeAurigmaTest(files[0]), 20, 2);
End:
Console.WriteLine("Done");
Console.ReadKey();
}
public static void RunResizeImageMagickTest(string source)
{
float[] ratios = { 0.25f, 0.8f, 1.4f };
// load the source bitmap
using (MagickImage bitmap = new MagickImage(source))
{
foreach (float ratio in ratios)
{
// determine the target image size
var size = new Size((int)Math.Round(bitmap.Width * ratio), (int)Math.Round(bitmap.Height * ratio));
MagickImage thumbnail = null;
try
{
thumbnail = new MagickImage(bitmap);
// scale the image down
thumbnail.Resize(size.Width, size.Height);
}
finally
{
if (thumbnail != null && thumbnail != bitmap)
{
thumbnail.Dispose();
}
}
}
}
}
public static void RunResizeAurigmaTest(string source)
{
float[] ratios = { 0.25f, 0.8f, 1.4f };
//// load the source bitmap
using (ABitmap bitmap = new ABitmap(source))
{
foreach (float ratio in ratios)
{
// determine the target image size
var size = new Size((int)Math.Round(bitmap.Width * ratio), (int)Math.Round(bitmap.Height * ratio));
ABitmap thumbnail = null;
try
{
thumbnail = new ABitmap();
// scale the image down
using (var resize = new Resize(size, InterpolationMode.HighQuality))
{
resize.ApplyTransform(bitmap, thumbnail);
}
}
finally
{
if (thumbnail != null && thumbnail != bitmap)
{
thumbnail.Dispose();
}
}
}
}
}
添加了用于测试的代码。我使用c#/。. NET, ImageMagick通过ImageMagick工作。Net库,Aurigma也有一个。对于IM。net lib是在c++/CLI上编写的,IM是C。OpenMP for IM已关闭。
可能是内存缓存问题。以某种方式使用内存的多个线程可能会创建一个场景,其中一个线程使另一个线程正在使用的缓存内存无效,从而导致停机。
不是纯粹的数字运算,而是依赖于大量IO (CPU<->Memory)的程序更难分析。