Click here to Skip to main content
15,917,060 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm streaming text files with stream read, But the code is not working efficiently. for example text file has max. 1000 lines but reading and writing data to db is takes about minutes...
In my project I'm reading text files line by line after that ,I'm adding this lines to a string arrayList. when I finish reading lines, I set lines to an object for writing data to my database.
This is my text reading code block;

List<string> dataList = new List<string>();
using (StreamReader sr = new StreamReader(file))
{
String line;
while ((line = sr.ReadLine()) != null)
{
if (!string.IsNullOrEmpty(line))
dataList.Add(line);
}
}


Can I make my codes faster?
And my codes second part is that takes more time ;



BankObject headerObj = new BankObject();

for (int i = 0; i
Posted

1 solution

Since the code you show only appears to use dataList as an array, why not just use
string[] dataList = File.ReadAllLines(file);
instead of your first loop - it will be much faster.

Your second loop is harder to optimize - it depends on what the data actually looks like to an extent. I do notice though that you don't check at all to ensure there is enough room in the string to extract a substring - what happens if the line from the file is 20 characters? It may be worth using a foreach loop rather than the for loop as it would get rid of the array access each time (although the non-debug build would optimize this anyway).

Don't use magic numbers unless you have to - they are a maintenance nightmare!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900