0

I wrote a program which works with stock quotes - receive market data, transform it and show in a richtextbox.

Why is my method which writes data to the richtextbox so slow?

This code don't calculate anything - just show the property value.

This method executes from 1 to 15 seconds...

public List <Svecha> spisoksvechek
public RichTextBox LogWind
public RichTextBox LogWind2

public void otrisovatSvechi()
{
    // this make an async work for code
    this.GuiAsync(() =>
    {
        // очищаем поля для вывода
        LogWind.Document.Blocks.Clear();
        LogWind2.Document.Blocks.Clear();

        //для каждой свечки в списке свечек
        foreach (Svecha sv in spisokSvechek)
        {
            // если количество сделок в свечке больше 0
            if (sv.sdelkiSvechi.Count > 0)
            {
                // выводим на поле1 начальное время свечи
                LogWind.AppendText(System.Environment.NewLine + sv.startCandleTime.ToString());

                // выводим на поле2 начальное время свечи
                LogWind2.AppendText(System.Environment.NewLine + sv.startCandleTime.ToString());

                // выводим на поле1 оборот свечи и дельту свечи
                LogWind.AppendText(System.Environment.NewLine + sv.volumeSvechi.ToString() + " x " + sv.deltaSvechi.ToString());

                // для каждого кластера в списке кластеров свечи
                foreach (var cl in sv.clusteruSvechi)
                {
                    // выводим на поле2 максимальную цену, минимальную цену, оборот и дельту кластера
                    LogWind2.AppendText(System.Environment.NewLine + cl.minPrice.ToString() + " - " + cl.maxPrice.ToString() + "  " + cl.clusterVolume.ToString() + " x " + cl.delta.ToString());
                }

            }
        }

    });

}
Tomalak
  • 332,285
  • 67
  • 532
  • 628
Vadym K
  • 25
  • 6
  • 1
    Instead of appending Text every time to the RichTextbox you should take a [StringBuilder](http://msdn.microsoft.com/en-us/library/system.text.stringbuilder(v=vs.110).aspx) append all text to this and afterwards assign text to RichTextbox only once! – Pilgerstorfer Franz Nov 14 '13 at 11:02
  • 2
    At first glance, all those string appends aren't good (or quick) - try switching to a `StringBuilder`. Also, is `spisokSvecheck` going to a datasource? You might find it's using lazy loading and only hitting the DB when you iterate the collection. – James Nov 14 '13 at 11:02
  • Try using a profiler to find out what lines take the most time. http://stackoverflow.com/questions/3927/what-are-some-good-net-profilers – Stormenet Nov 14 '13 at 11:03
  • It's quite hard to make any informed comments in vacuum. How can we know if enumerator for `sv.clusteruSvechi` doesn't call `Thread.Sleep`? Other than that, definitely try to use a StringBuilder instead of multiple string concatenations. – decPL Nov 14 '13 at 11:07
  • thnak you all. I'll try stringbuilder. spisokSvecheck is a list with class which incapsulate startTime and endTime (DataTime) properties. It don't go to datasource. – Vadym K Nov 14 '13 at 11:12
  • Think about using `ListBox` controls and `.Items.Add(someString)` instead of appending stuff to a `RichTextBox`. For debug output this should work nicely and perform well. – Tomalak Nov 14 '13 at 13:51

0 Answers0