my program works very slow
-
hi guys...i get some informations from a site Xml file and convert that file to DataTable and then save it in my form's load..it works well but the problem it works very very slow..when i clicked the button to open my form it takes one min almost to open it..how can i make it faster to work..here is the codes i wrote...
public XmlNodeList XmlListesi()
{
XmlDocument doc = new XmlDocument();
doc.Load("http://cekilis.millipiyango.gov.tr/haftalik\_loto\_bilgileri.xml");
XmlElement root = doc.DocumentElement;
XmlNodeList elemlist = root.GetElementsByTagName("haftalik_loto_bilgileri");
return elemlist;
}public DataTable ConvertXmlNodeListToDataTable(XmlNodeList xnl) { DataTable tablo = new DataTable(); int TempColumn = 0; foreach (XmlNode item in xnl.Item(0).ChildNodes) { TempColumn++; DataColumn dc = new DataColumn(item.Name, System.Type.GetType("System.String")); if (tablo.Columns.Contains(item.Name)) tablo.Columns.Add(dc.ColumnName = dc.ColumnName + TempColumn.ToString()); else tablo.Columns.Add(dc); } int ColumnsCount = tablo.Columns.Count; for (int i = 0; i < xnl.Count; i++) { DataRow dr = tablo.NewRow(); for (int j = 0; j < ColumnsCount; j++) { try { dr\[j\] = xnl.Item(i).ChildNodes\[j\].InnerText; } catch (Exception) { } } tablo.Rows.Add(dr); } return tablo; } public void InsertSayisal(DataTable dt) { cmd = dal.InsertSayisal(); cmd.Parameters.AddWithValue("@cek\_no", System.Data.SqlDbType.Int); cmd.Parameters.AddWithValue("@cek\_tarih", System.Data.SqlDbType.NVarChar); cmd.Parameters.AddWithValue("@num1", System.Data.SqlDbType.NVarChar); cmd.Parameters.AddWithValue("@num2", System.Data.SqlDbType.NVarChar); cmd.Parameters.AddWithValue("@num3", System.Data.SqlDbType.NVarChar); cmd.Parameters.AddWithValue("@num4", System.Data.SqlDbType.NVarChar); cmd.Parameters.AddWithValue("@num5", System.Data.SqlDbType.NVarChar); cm
-
hi guys...i get some informations from a site Xml file and convert that file to DataTable and then save it in my form's load..it works well but the problem it works very very slow..when i clicked the button to open my form it takes one min almost to open it..how can i make it faster to work..here is the codes i wrote...
public XmlNodeList XmlListesi()
{
XmlDocument doc = new XmlDocument();
doc.Load("http://cekilis.millipiyango.gov.tr/haftalik\_loto\_bilgileri.xml");
XmlElement root = doc.DocumentElement;
XmlNodeList elemlist = root.GetElementsByTagName("haftalik_loto_bilgileri");
return elemlist;
}public DataTable ConvertXmlNodeListToDataTable(XmlNodeList xnl) { DataTable tablo = new DataTable(); int TempColumn = 0; foreach (XmlNode item in xnl.Item(0).ChildNodes) { TempColumn++; DataColumn dc = new DataColumn(item.Name, System.Type.GetType("System.String")); if (tablo.Columns.Contains(item.Name)) tablo.Columns.Add(dc.ColumnName = dc.ColumnName + TempColumn.ToString()); else tablo.Columns.Add(dc); } int ColumnsCount = tablo.Columns.Count; for (int i = 0; i < xnl.Count; i++) { DataRow dr = tablo.NewRow(); for (int j = 0; j < ColumnsCount; j++) { try { dr\[j\] = xnl.Item(i).ChildNodes\[j\].InnerText; } catch (Exception) { } } tablo.Rows.Add(dr); } return tablo; } public void InsertSayisal(DataTable dt) { cmd = dal.InsertSayisal(); cmd.Parameters.AddWithValue("@cek\_no", System.Data.SqlDbType.Int); cmd.Parameters.AddWithValue("@cek\_tarih", System.Data.SqlDbType.NVarChar); cmd.Parameters.AddWithValue("@num1", System.Data.SqlDbType.NVarChar); cmd.Parameters.AddWithValue("@num2", System.Data.SqlDbType.NVarChar); cmd.Parameters.AddWithValue("@num3", System.Data.SqlDbType.NVarChar); cmd.Parameters.AddWithValue("@num4", System.Data.SqlDbType.NVarChar); cmd.Parameters.AddWithValue("@num5", System.Data.SqlDbType.NVarChar); cm
Hi, I don't have the answer, I do have a lot of questions and some suggestions: 1. how many rows and columns are in your DataTable? 2. why are you swallowing exceptions? it is dumb to do so, as it hides potential or real problems you are having. Log them to a log file, and look at it. Don't use MessageBoxes, they are just annoying. Use at most one, at the end of an operation, to say something went wrong and more details are in the log file. 3. why are you storing almost everything as a string? it wastes time and disk space, and it probably hampers functionality too. 4. why are you repeating the same operations all the time? e.g. I see lots of dt.Rows[i] you would better fetch the row once and keep it in a local variable. 5. I can't tell how long dal.InsertSayisal() should take, you should look into that method too. 6. you could use a StopWatch to get timing information, and log that to your log file, together with some extra information; that way you could see how your code behaves, i.e. where exactly time gets wasted. 7. if, I don't know, your DataTable's only purpose is to get the XML data to the database, then maybe you would be better off doing it in a single loop, one row at a time, avoiding the need to keep everything in memory at once. (Depends on #1). :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
hi guys...i get some informations from a site Xml file and convert that file to DataTable and then save it in my form's load..it works well but the problem it works very very slow..when i clicked the button to open my form it takes one min almost to open it..how can i make it faster to work..here is the codes i wrote...
public XmlNodeList XmlListesi()
{
XmlDocument doc = new XmlDocument();
doc.Load("http://cekilis.millipiyango.gov.tr/haftalik\_loto\_bilgileri.xml");
XmlElement root = doc.DocumentElement;
XmlNodeList elemlist = root.GetElementsByTagName("haftalik_loto_bilgileri");
return elemlist;
}public DataTable ConvertXmlNodeListToDataTable(XmlNodeList xnl) { DataTable tablo = new DataTable(); int TempColumn = 0; foreach (XmlNode item in xnl.Item(0).ChildNodes) { TempColumn++; DataColumn dc = new DataColumn(item.Name, System.Type.GetType("System.String")); if (tablo.Columns.Contains(item.Name)) tablo.Columns.Add(dc.ColumnName = dc.ColumnName + TempColumn.ToString()); else tablo.Columns.Add(dc); } int ColumnsCount = tablo.Columns.Count; for (int i = 0; i < xnl.Count; i++) { DataRow dr = tablo.NewRow(); for (int j = 0; j < ColumnsCount; j++) { try { dr\[j\] = xnl.Item(i).ChildNodes\[j\].InnerText; } catch (Exception) { } } tablo.Rows.Add(dr); } return tablo; } public void InsertSayisal(DataTable dt) { cmd = dal.InsertSayisal(); cmd.Parameters.AddWithValue("@cek\_no", System.Data.SqlDbType.Int); cmd.Parameters.AddWithValue("@cek\_tarih", System.Data.SqlDbType.NVarChar); cmd.Parameters.AddWithValue("@num1", System.Data.SqlDbType.NVarChar); cmd.Parameters.AddWithValue("@num2", System.Data.SqlDbType.NVarChar); cmd.Parameters.AddWithValue("@num3", System.Data.SqlDbType.NVarChar); cmd.Parameters.AddWithValue("@num4", System.Data.SqlDbType.NVarChar); cmd.Parameters.AddWithValue("@num5", System.Data.SqlDbType.NVarChar); cm
Use XmlTextReader instead of XmlDocument. XmlDocument will sit there reading and indexing the whole document before you can start working with it. XmlTextReader will stream the data so you can work while it is reading. Just note XmlTextReader is harder to work with.
"You get that on the big jobs."
-
hi guys...i get some informations from a site Xml file and convert that file to DataTable and then save it in my form's load..it works well but the problem it works very very slow..when i clicked the button to open my form it takes one min almost to open it..how can i make it faster to work..here is the codes i wrote...
public XmlNodeList XmlListesi()
{
XmlDocument doc = new XmlDocument();
doc.Load("http://cekilis.millipiyango.gov.tr/haftalik\_loto\_bilgileri.xml");
XmlElement root = doc.DocumentElement;
XmlNodeList elemlist = root.GetElementsByTagName("haftalik_loto_bilgileri");
return elemlist;
}public DataTable ConvertXmlNodeListToDataTable(XmlNodeList xnl) { DataTable tablo = new DataTable(); int TempColumn = 0; foreach (XmlNode item in xnl.Item(0).ChildNodes) { TempColumn++; DataColumn dc = new DataColumn(item.Name, System.Type.GetType("System.String")); if (tablo.Columns.Contains(item.Name)) tablo.Columns.Add(dc.ColumnName = dc.ColumnName + TempColumn.ToString()); else tablo.Columns.Add(dc); } int ColumnsCount = tablo.Columns.Count; for (int i = 0; i < xnl.Count; i++) { DataRow dr = tablo.NewRow(); for (int j = 0; j < ColumnsCount; j++) { try { dr\[j\] = xnl.Item(i).ChildNodes\[j\].InnerText; } catch (Exception) { } } tablo.Rows.Add(dr); } return tablo; } public void InsertSayisal(DataTable dt) { cmd = dal.InsertSayisal(); cmd.Parameters.AddWithValue("@cek\_no", System.Data.SqlDbType.Int); cmd.Parameters.AddWithValue("@cek\_tarih", System.Data.SqlDbType.NVarChar); cmd.Parameters.AddWithValue("@num1", System.Data.SqlDbType.NVarChar); cmd.Parameters.AddWithValue("@num2", System.Data.SqlDbType.NVarChar); cmd.Parameters.AddWithValue("@num3", System.Data.SqlDbType.NVarChar); cmd.Parameters.AddWithValue("@num4", System.Data.SqlDbType.NVarChar); cmd.Parameters.AddWithValue("@num5", System.Data.SqlDbType.NVarChar); cm
Ditch the
DataTable
. Ditch all thoseToString
s. -
Ditch the
DataTable
. Ditch all thoseToString
s.thanks for your suggestions guys..i will change something in my codes by your suggesstions