为什么我在跨多个线程运行并发 Graphics.RotateTransform 操作时没有看到显着的速度提升?
此示例展示了边际性能提升:
static Bitmap rotate(Bitmap bitmap, float angle)
{
Bitmap rotated = new Bitmap(bitmap.Width, bitmap.Height);
using (Graphics g = Graphics.FromImage(rotated))
{
g.TranslateTransform((float)bitmap.Width / 2, (float)bitmap.Height / 2);
g.RotateTransform(angle);
g.TranslateTransform(-(float)bitmap.Width / 2, -(float)bitmap.Height / 2);
g.DrawImage(bitmap, new Point(0, 0));
}
return rotated;
}
static void Main(string[] args)
{
WebClient client = new WebClient();
string fileName = Path.GetTempFileName();
client.DownloadFile("http://i.imgur.com/Vwiul.png", fileName);
Bitmap original = new Bitmap(fileName);
DateTime start = DateTime.Now;
rotateSingleThread(original);
TimeSpan length = DateTime.Now - start;
Console.WriteLine(string.Format("Single thread rotate took {0} ms", (int)length.TotalMilliseconds));
start = DateTime.Now;
rotateMultiThread(original);
length = DateTime.Now - start;
Console.WriteLine(string.Format("Multiple thread rotate took {0} ms", (int)length.TotalMilliseconds));
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
static void rotateSingleThread(Bitmap original)
{
for (float a = 0; a < 360; a += 0.1F)
{
Bitmap rotated = rotate(original, a);
rotated.Dispose();
}
}
static void rotateMultiThread(Bitmap original)
{
int threadCount = Environment.ProcessorCount;
int groupSize = 360 / threadCount;
Thread[] threads = new Thread[threadCount];
Bitmap[] bitmaps = new Bitmap[threadCount];
for (int x = 0; x < threadCount; x++)
{
bitmaps[x] = new Bitmap(original);
threads[x] = new Thread(delegate(object i)
{
int min = (int)i * groupSize; int max = min + groupSize;
if ((int)i == threadCount - 1) max = 360;
for (float a = min; a < max + 1; a+= 0.1F)
{
Bitmap rotated = rotate(bitmaps[(int)i], a);
rotated.Dispose();
}
});
threads[x].Start(x);
}
for (int x = 0; x < threadCount; x++) threads[x].Join();
}
如何提高执行并发 Graphics.RotateTransform 操作的速度?
问题是那些 GDI+ 调用会按进程(而不是按线程)阻塞,因此即使线程并行运行,对 graphics....
的调用也会导致其他线程阻塞。这意味着它最终几乎是串行处理。
请参阅下面问题中的各种解决方法,最上面的一个建议创建单独的进程。看起来您最终可能只是创建一个辅助命令行实用程序或其他东西。您甚至可以将 stdin 和 stdout 连接起来,这样您就可以将图像传递给旋转并在没有 IO 操作的情况下取回新图像。
Parallelizing GDI+ Image Resizing .net
我是一名优秀的程序员,十分优秀!