🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.pl.comp.lang.csharp
Article #22389

Re: better performance for algorithm

#22389
From: Łukasz 'Maly' Ostrowski
Date: Thu, 26 Apr 2012 19:16
35 lines
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/
Technical Details
Message-ID: <1nk7raeti6icj$.rnuf9ujeu2my$.dlg@40tude.net>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!newsfeed.pionier.net.pl!news.dialog.net.pl!not-for-mail
References: <01555af6-7cde-4611-82f5-a4f97b81d87a@f37g2000yqc.googlegroups.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!newsfeed.pionier.net.pl!news.dialog.net.pl!not-for-mail
From: Łukasz 'Maly' Ostrowski <maly@USUNnemo.pl>
Newsgroups: alt.pl.comp.lang.csharp
Subject: Re: better performance for algorithm
Date: Thu, 26 Apr 2012 21:16:34 +0200
Organization: Acme
Lines: 35
Message-ID: <1nk7raeti6icj$.rnuf9ujeu2my$.dlg@40tude.net>
References: <01555af6-7cde-4611-82f5-a4f97b81d87a@f37g2000yqc.googlegroups.com>
NNTP-Posting-Host: static-78-9-71-129.ssp.dialog.net.pl
Mime-Version: 1.0
X-Trace: news.dialog.net.pl 1335467802 13250 78.9.71.129 (26 Apr 2012 19:16:42 GMT)
X-Complaints-To: abuse@dialog.net.pl
NNTP-Posting-Date: Thu, 26 Apr 2012 19:16:42 +0000 (UTC)
User-Agent: 40tude_Dialog/2.0.15.1
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit