Performance Itextsharp

Asked

Viewed 142 times

3

I’m doing the reports using iTextsharp and the result has been acceptable, but working with many records has become slow. Currently I do so:

I capture the database data (storing in a DataTable);

In a foreach concateno all in one string: (ex tosco)

foreach (DataRow row in FaturamentoCorpo.Rows)
  stringao += row["ncodigfilia"].ToString()

At the end game for iTextsharp this "stringão":

Paragraph paragrafo = new Paragraph("", new Font(Font.FontFamily.COURIER, FontSize));
paragrafo.Add(stringao);

So this loop concatenating into the string, when the DataTable has ai +- 10k records, resulting in a PDF of 70 to 80 pages, it is kind of time consuming.

So I wanted a better way to work that?

  • have tried: foreach (DataRow row in FaturamentoCorpo.Rows)
 paragrafo.Add(row["ncodigfilia"].ToString()); ? even if it is not in the same format, just to check the performance ?!

  • 1

    His comment was simple, but punctual, which complementing with the following answer I managed to solve the problem. Thank you very much.

1 answer

2


I answered about the problem in another answer (another).

You can’t do it because the more you concatenate, the slower it gets, and it’s exponential, and it quickly becomes tragic. Then I’d have to do something like this:

var todasLinhas = new StringBuilder();
foreach (DataRow row in FaturamentoCorpo.Rows)
    todasLinhas += row["ncodigfilia"].ToString();
var paragrafo = todasLinhas.ToString();

I put in the Github for future reference.

Ideally it would be good to try to find out at least the total approximate size that this string will have and instantiate the StringBuilder about this size. It seems possible since you know how many lines there are and imagine that you can know, if not exact, at least approximate the size of each line.

One more explanation.

Researching links I’m starting to find duplicate.

  • 1

    Thank you, the previous comment solved 70% of the problem, and in the links q Oce passed on the performance of string type (things I was unaware of), mainly the fact of immutability,:

  • 1: Concatenate the datatable fields into a stringbuilder; 2: Send each row to the Paragraf variable, zeroing stringbuilder; Not yet tested change 1, pq will imply the entire system, so precise time, change 2 using common string looks great.. I think I’ll be even better.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.