具有第二个代码:
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%。这是Core i7 8700。
使它运行更快的唯一方法是启动应用程序的第二个窗口。
这是窗户限制吗?使用Windows Server 2016。
我认为这是系统限制,因为如果我尝试这个简单的代码,它也将绑定12%的CPU!
while (true)
{
var a = 1 + 2;
}
具有第二个代码:类方法{public MemoryStream UniqPicture(string imagePath){var photoBytes = File.ReadAllBytes(imagePath); //用有效的图像路径更改imagePath ...
一些研究表明,您正在使用https://imageprocessor.org/中的ImageFactory,其中包含System.Drawing。 System.Drawing本身通常是GDI / GDI +的包装,它包含了进程范围的锁,因此您在尝试多线程时将受到严重的瓶颈。尝试更好的图片库。
(请参阅Robert McKee的答案,尽管也许这可能与磁盘IO有关,但也许不是。)