如何加快我的代码 [英] how to speed my code up

查看:76
本文介绍了如何加快我的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨...
我在C#中有一个方法,但是它工作非常缓慢,我该如何加快速度?!

这是我的代码:

Hi...
I have a methode in c# but it works very slowly,,,how I can speed it up?!

it''s my code:

private void button1_Click(object sender, EventArgs e)
        {
                        richTextBox1.Font = new Font("Scheherazade", 12, FontStyle.Bold);
            richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
            int int1 = int.Parse(textBox1.Text);
            try
            {
                foreach (Database24DataSet._Quran_simple___1Row row in qs.GetData())
                    if ((int)qs.selectsurrah(int1) == row.Surrah && (int)qs.selectsurrah(int1) < row.Surrah + 1)
                    {
                        richTextBox1.Text = richTextBox1.Text + "\n" + row.Arabic;
                        Application.DoEvents();
                    }
            }
            catch { }
        }
    }
}




例如:selectsurrah是一个查询,我在文本框中输入一个数字,然后选择一些记录并添加到richtextbox
:




for example: selectsurrah is a query,I enter a number in a textbox and it select some record and add into richtextbox
:

SELECT        Surrah
FROM            [Quran-simple - 1]
WHERE        (Surrah = ?)





帮帮我吧!
谢谢...





help me!
thanks...

推荐答案

隐式转换很慢,请尝试通过.NET Class Convert进行显式转换.
在richtextboxcontrol的设计时在属性窗口上设置字体属性.

对于集合中的每一行,该行代码都会被调用两次,因此您的性能将会非常慢.因此,这是您代码的主要问题.
Implicit conversion is slow, Try with Explicit conversion via .NET Class Convert.
Set your Font properties on properties windows in design time of your richtextboxcontrol.

This line of code is called twice for every row in your collection, so you will have a very slow performance. So, here is the Main problem of your code.
if ((int)qs.selectsurrah(int1) == row.Surrah && (int)qs.selectsurrah(int1) < row.Surrah + 1)


试试这个:


Try this one:

private void button1_Click(object sender, EventArgs e)
{
    int int1 = 0;
    int.TryParse(textBox1.Text,out int1);

    int int_from_select = qs.selectsurrah(int1);

    foreach(Database24DataSet._Quran_simple___1Row row in qs.GetData())
    {
        if (row.Surrah.Equals(int_from_select) && int_from_select <(row.Surrah+1)) //Prevents to call a SELECT statement for each If
        {
            richTextBox1.Text = richTextBox1.Text + "\n" + row.Arabic;
            Application.DoEvents();
        }   
    }
}



希望能对您有所帮助.



Hope it helps.


正如韦斯所说,很难弄清无法运行的速度.
但是,您应该做一些事情:
1)永远不要调用Application.DoEvents-如果该过程很慢,则将其移至后台工作线程中.
2)不要串联字符串:请改用StringBuilder.每次执行string1 = string2 + string3时,都会分配一个足以容纳string2和string3的新内存区域,然后将两个字符串都复制到其中.如果您有很多行,那么这可能是一个非常缓慢的过程-使用StringBuilder,它仅在需要时才分配新内存.

查看 Stopwatch类 [
As Wes says, it is very difficult to work out what is slow when we can''t run it.
But, there are a few things you should do:
1) Never call Application.DoEvents - if the process is that slow, then move it into a background worker thread instead.
2) Don''t concatenate strings: use a StringBuilder instead. Each time you do string1 = string2 + string3, you allocate a new area of memory big enough to hold string2 and string3 then copy both strings into it. If you have many rows, then this can be a very slow process - use a StringBuilder which only allocates new memory when it needs to.

Look at the Stopwatch class[^] - it can give you an idea what it taking time, and at least give you something to measure your improvemnets against.


如果要显示很多文本,甚至是这样:
If there is a lot of text to be displayed then even this :
richTextBox1.Text = richTextBox1.Text + "\n" + row.Arabic;


比这慢:


is slower than this:

richTextBox1.Text = richTextBox1.Text + string.format("\n{0}",row.Arabic);



甚至使用stringbuilder并仅更新GUI都说每100毫秒更好.



Or even using a stringbuilder and only updating the GUI say every 100ms is better.


这篇关于如何加快我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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