优化代码以不占用大量CPU使用率 [英] optimizing the code not to use much CPU usage

查看:108
本文介绍了优化代码以不占用大量CPU使用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我编写了以下代码,以便从台式机获取大量图像以制作包含图像的视频,它可以工作,但是CPU使用率约为80%或90%.我试图使它使用较低的CPU,但我做不到.有没有一种方法可以优化代码以不占用那么多CPU?
我的CPU是Intel双核2200

请帮忙.

Hi,
I''ve written the code below in order to take numbers of images from desktop to make a video with images, it works, but CPU usage is about 80 or 90%. I tried to make it use lower CPU but I couldn''t. Is there a way to optimize the code to not use that much of CPU?
My CPU is intel dual core 2200

Please help.

private int num;
private void timer1_Tick(object sender, EventArgs e)
{ 
SendKeys.Send("{PRTSC}");
Image i = Clipboard.GetImage(); 
i.Save("pics\\" + num, System.Drawing.Imaging.ImageFormat.Png); 
num++; 
}




我在下面使用100计时器间隔的代码,但CPU使用率超过90%:





I''v used code below with 100 as timer interval but cpu usage gets over 90% :


int ScreenWidth = Screen.PrimaryScreen.Bounds.Width;
int ScreenHeight = Screen.PrimaryScreen.Bounds.Height;
Graphics g;
Bitmap b = new Bitmap(ScreenWidth, ScreenHeight);
g = Graphics.FromImage(b);
g.CopyFromScreen(Point.Empty, Point.Empty, Screen.PrimaryScreen.Bounds.Size);
b.Save("pics//" + num++);




这是另一个使用20%cpu的代码,没关系,它的内存使用量约为20 mb,但是经过1分钟后突然超过了1.5 GB !!!为什么呢? 代码:




this is another code that uses 20% of the cpu , its okay,its memory usage is around 20 mb but after 1minute and suddenly it goes over 1.5 GB!!! why is that??!!
code :

Int64 num;
Bitmap[] bit = new Bitmap[200000000];
int ScreenWidth = Screen.PrimaryScreen.Bounds.Width;
int ScreenHeight = Screen.PrimaryScreen.Bounds.Height;
Graphics g;
Bitmap b = new Bitmap(ScreenWidth, ScreenHeight);
g = Graphics.FromImage(b);
g.CopyFromScreen(Point.Empty, Point.Empty, Screen.PrimaryScreen.Bounds.Size);
bit[num++] = b;






谢谢






Thanks

推荐答案

^ ]

请检查此链接.我想这对您有帮助

此应用程序肯定会占用您100%的RAM,但使用的CPU更少.

要改进,可以使用PFX或RX.
http://kishordgupta.wordpress.com/2011/02/01/step-by-step-to-create-a-desktop-video-recorder-in-c/[^]

Please check this link. I think this will help you

This app will definitely utilize your 100% RAM but use less CPU.

To improve you can use PFX or RX.


不需要将图像复制到剪贴板,2irfanshaikh给出的链接显示了一种避免这种情况的简单方法.

我还指出,您正在将图像另存为png文件-这确实使文件大小令人惊讶,但是压缩需要使用cpu.这可能是花费大多数CPU时间的地方.尝试尝试其他格式.

最好的问候
Espen Harlinn
There is no need to copy the image to the clipboard, the link given by 2irfanshaikh shows a simple way to avoid this.

I also noted that you are saving your image as a png file - this does wonders for the file size, but the compression requires cpu. It''s probably where most of cpu time is spent. Try experimenting with other formats.

Best regards
Espen Harlinn


只需添加,您的代码就会泄漏内存.只要对象具有Dispose方法,就应使用"using"语句,以便自动清理内存.这既是Graphics对象又是Bitmap对象.
Just to add, your code leaks memory. You should use ''using'' statements where-ever an object has a Dispose method, in order to clean up memory automatically. That''s both the Graphics and the Bitmap objects.


这篇关于优化代码以不占用大量CPU使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆