c# - 为什么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;
}
解决方案
一些研究表明您正在使用来自https://imageprocessor.org/的 ImageFactory ,它包装了 System.Drawing。System.Drawing 本身通常是 GDI/GDI+ 的包装器,它...包含进程范围的锁,因此您对多线程的尝试将受到严重的瓶颈。尝试更好的图像库。
推荐阅读
- autodesk-forge - 是否有基于 Forge 视图过滤 Revit 元素的选项?
- stm32 - STM32:将 USART 与字符匹配 ISR 和 DMA 缓冲区一起使用
- r - Rmarkdown中的插入符号doparallel:使用render()时缺少详细信息
- excel - Excel VBA - 在某些条件下删除行
- laravel - Laravel:在关闭时翻译验证消息
- php - 如何在php中搜索和读取excel文件
- c# - 是否有像 C++ 中那样带有分隔符的 C# 原始字符串?
- python - Python:编解码器和 utf-8 全局错误处理
- jquery-select2 - 如何在 select2 中有选项组时使用 Ajax 插入选项?
- javascript - 删除鼠标点击并为简单的 Javascript 添加边框