首页 > 解决方案 > 为什么CPU使用率不增加?

问题描述

有第二个代码:

class Methods
{
    public MemoryStream UniqPicture(string imagePath)
    {
        var photoBytes = File.ReadAllBytes(imagePath); // change imagePath with a valid image path
        var quality = 70;
        var format = ImageFormat.Jpeg; // we gonna convert a jpeg image to a png one
        var size = new Size(200, 200);

        using (var inStream = new MemoryStream(photoBytes))
        {
            using (var outStream = new MemoryStream())
            {

                using (var imageFactory = new ImageFactory())
                {

                    imageFactory.Load(inStream)
                        .Rotate(new Random().Next(-7, 7))
                        .RoundedCorners(new RoundedCornerLayer(190))
                        .Pixelate(3, null)
                        .Contrast(new Random().Next(-15, 15))
                        .Brightness(new Random().Next(-15, 15))
                        .Quality(quality)
                        .Save(outStream);
                }

                return outStream;
            }
        }
    }

    public void StartUniq()
    {
        var files = Directory.GetFiles("mypath");
        Parallel.ForEach(files, (picture) => { UniqPicture(picture); });
    }

}

当我开始方法 StartUniq() 我的 CPU 绑定到 12-13% 并且没有更多。我可以使用更多的 CPU % 来执行此操作吗?为什么不增加?

我尝试用 python 来做,也只有 12-13%。它是酷睿 i7 8700。

让它运行得更快的唯一方法是启动应用程序的第二个窗口。

是窗口限制吗?使用 Windows Server 2016。

我认为这是系统限制,因为如果我尝试这个简单的代码,它也会绑定 12% 的 CPU!

 while (true)
        {
            var a = 1 + 2;
        }

标签: c#windowsmultithreadingconcurrencycpu

解决方案


一些研究表明您正在使用来自https://imageprocessor.org/的 ImageFactory ,它包装了 System.Drawing。System.Drawing 本身通常是 GDI/GDI+ 的包装器,它...包含进程范围的锁,因此您对多线程的尝试将受到严重的瓶颈。尝试更好的图像库。


推荐阅读