预先控制开销 [英] foreach overhead control
问题描述
你好
i我是初学者在c#
我的项目中的一个函数就像下面的代码
但是增长widthforeache's lopps长宽^ 5,我的表格挂断
请帮帮我怎么办:(
hello
i am beginner in c#
one of my functions in my project is like below code
but with grow "width" foreache's lopps grow with width ^5 and my form hang up
please help me what should i do :(
private void ASCI4(double[] x, int width)
{
double[] up = new double[width];
double[] lp = new double[width];
double[] ud = new double[width];
double[] ld = new double[width];
for(int i=0;i<width;i++)
{
up[i]=x[i]+0.25;
UP.Add(up[i]);
lp[i]=x[i]-0.25;
LP.Add(lp[i]);
ud[i]=x[i]+0.5;
UD.Add(ud[i]);
ld[i] = x[i] - 0.5;
LD.Add(ld[i]);
}
foreach (double up1 in UP)
{
foreach (double ud1 in UD)
{
foreach (double lp1 in LP)
{
foreach (double ld1 in LD)
{
foreach (Point v1 in V)
{
if ((v1.Y > lp1) & (v1.Y < up1))
RP.Add(v1.Y);
else if ((v1.Y < ld1) | (v1.Y > ud1))
RN.Add(v1.Y);
else if (((v1.Y >= ld1) & (v1.Y <= lp1)) | ((v1.Y >= up1) & (v1.Y <= ud1)))
RZ.Add(v1.Y);
}
}
}
}
}
}
推荐答案
目前您的算法是设计以实现这样的行为。
所以你要么是要
As it stands your algorithm is designed for following such a behaviour.
So you have either to
- 接受这个事实。
- 重新考虑算法的正确性(或者如果有更智能的算法能够满足您的要求)
如果你没有说明你的确切要求,我们就无法帮助你。
We cannot help on point 2 if you don't state what are your exact requirements.
正如我五天前建议的那样:在调试时支持visual studio [ ^ ]如果您想阻止此代码挂断您的用户接口,然后你需要将它移动到一个不同的线程。再看看我给你的BackgroundWorker链接: http://msdn.microsoft.com /en-us/library/system.componentmodel.backgroundworker.aspx [ ^ ] - 它真的不复杂!
OriginalGriff:我在那个链接中阅读了这篇文章,但我没有说明它是什么以及我在我的代码中如何使用它:(
我不是傻到我是初学者这是课堂项目:D;)
跟随这个例子有多难?
所有你需要的是四行代码和两个事件处理程序!
As I suggested five days ago: hang up visual studio when debugging[^] if you want to stop this code from hanging up your user interface, then you need to move it into a different thread. Look again at the BackgroundWorker link I gave you then: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[^] - it realy isn't complicated!
"OriginalGriff: I read the article in that link but i didn't undrestand what does it exactly do and how i use it in my code :(
i'm not stupid just i'm beginner and this is class project :D ;)"
How difficult is it to follow the example?
All you need is four lines of code and two event handlers!
using System;
using System.ComponentModel;
namespace OneOffConsole
{
class Program
{
static void Main(string[] args)
{
BackgroundWorker work = new BackgroundWorker();
work.DoWork += new DoWorkEventHandler(work_DoWork);
work.RunWorkerCompleted += new RunWorkerCompletedEventHandler(work_RunWorkerCompleted);
work.RunWorkerAsync();
Console.ReadLine();
}
static void work_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// All done - show your results here.
}
static void work_DoWork(object sender, DoWorkEventArgs e)
{
// Call your long running method here
}
}
}
就是这样。这是WinForms所需的相同代码 - 这只是一个控制台应用程序,所以我可以向您展示整个程序!
它的作用是将长期运行的任务移到不同的线程,所以它不会堵塞你的主线程 - 这意味着你的用户界面继续工作并响应而另一个线程完成工作 - 这有点像交出阅读书籍和写一页的工作总结给朋友,继续你的生活。然后朋友告诉你他什么时候完成,你可以查看他产生的摘要。
That is it. It's the same code needed for WinForms - this is just a console app so I can show you the entire program!
What it does is move your long running task onto a different thread, so it proceeds without "clogging up" your main thread - which mean that your user interface continues to work and respond while the other thread does the work - it's a bit like handing the job of reading a book and writing a one page summary off to a friend and getting on with your life. The friend then lets you know when he is finished, and you can view the summary he has produced.
这篇关于预先控制开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!