Thread View: alt.pl.comp.lang.csharp
2 messages
2 total messages
Started by kazik
Thu, 26 Apr 2012 18:54
better performance for algorithm
Author: kazik
Date: Thu, 26 Apr 2012 18:54
Date: Thu, 26 Apr 2012 18:54
21 lines
1870 bytes
1870 bytes
I have something like this:
DataTable t = new DataTable();
a.Fill(t);
for (int i = 0; i < t.Rows.Count; i++)
{
txt += "\n" + t.Rows[i][0] + "\t" + t.Rows[i][1] + "\t" + t.Rows[i][2]
+ "\t" + t.Rows[i][3] + "\t" + t.Rows[i][4] + "\t" + t.Rows[i][5] +
"\t" + t.Rows[i][6] + "\t" + t.Rows[i][7] + "\t" + t.Rows[i][8] + "\t"
+ t.Rows[i][9] + "\t" + t.Rows[i][10] + "\t" + t.Rows[i][11] + "\t" +
t.Rows[i][12] + "\t" + t.Rows[i][13] + "\t" + t.Rows[i][14] + "\t" +
t.Rows[i][15] + "\t" + t.Rows[i][16] + "\t" + t.Rows[i][17] + "\t" +
t.Rows[i][18] + "\t" + t.Rows[i][19];
}
I need this tabs between columns to generate txt file later.
Problem is that t.Rows.Count = 600000 and this loop works 9 hours.
Any ideas how to make this faster?
Regards
kazik
Re: better performance for algorithm
Author: Łukasz 'Maly' Ostrowski
Date: Thu, 26 Apr 2012 19:16
Date: Thu, 26 Apr 2012 19:16
35 lines
2262 bytes
2262 bytes
On Thu, 26 Apr 2012 11:54:58 -0700 (PDT), kazik wrote:
> DataTable t = new DataTable();
> a.Fill(t);
> for (int i = 0; i < t.Rows.Count; i++)
> {
> txt += "\n" + t.Rows[i][0] + "\t" + t.Rows[i][1] + "\t" + t.Rows[i][2]
> + "\t" + t.Rows[i][3] + "\t" + t.Rows[i][4] + "\t" + t.Rows[i][5] +
> "\t" + t.Rows[i][6] + "\t" + t.Rows[i][7] + "\t" + t.Rows[i][8] + "\t"
> + t.Rows[i][9] + "\t" + t.Rows[i][10] + "\t" + t.Rows[i][11] + "\t" +
> t.Rows[i][12] + "\t" + t.Rows[i][13] + "\t" + t.Rows[i][14] + "\t" +
> t.Rows[i][15] + "\t" + t.Rows[i][16] + "\t" + t.Rows[i][17] + "\t" +
> t.Rows[i][18] + "\t" + t.Rows[i][19];
> }
>
> I need this tabs between columns to generate txt file later.
> Problem is that t.Rows.Count = 600000 and this loop works 9 hours.
> Any ideas how to make this faster?
I assume txt is a string variable, use a StringBuilder instead, and
preallocate it if you can even roughly estimate the total string
length. Most of the execution time is consumed by string
reallocations - whenever in doubt, use a profiler...
...or just push the data to the destination file directly instead of
buffering it in a huge string and then saving it.
You might as well cache the DataRow in a Variable so the t.Rows
indexer propertny isn't called repeatedly for a single row.
p.s. To jest polsko-języczna grupa ;-).
--
Pozdrawiam,
Łukasz 'Maly' Ostrowski. http://l3v.pl/
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads