VB.NET For Loop too slow when grabbing XML -


i getting values xml file (a resources file), , inserting them datatable. have 679 keys resource file, , takes 3.41 seconds. wondering if there way of making loop faster.

i have tried parallel.for loop, found unstable because begins inserting row when previous insert didn't finish. used synch block, speed went 3.41.

 idx integer = 0 keynames.length - 1         keymanagerresource.instance             datatablemanager.instance.insertrow(keynames(idx), .getkeyvalue(keynames(idx), dynamicproperties.instance.englishresourcepath), _                                                                .getkeyvalue(keynames(idx), dynamicproperties.instance.frenchresourcepath))         end     next  ''' <summary> ''' gets value of key. ''' </summary> ''' <param name="id">id of key.</param> ''' <returns>value of key.</returns> ''' <remarks></remarks> overrides function getkeyvalue(id string, file string) string     'sets current path of xmlreader english file.     xmlmanager.instance.setreaderpath(file)      dim returnednode xml.xmlnode = xmlmanager.instance.getnode(string.format("//data" & helper.caseinsensitivesearch("name"), "'" & id.tolower & "'"))      if returnednode isnot nothing         return returnednode.childnodes(1).innertext     else         return ""     end if  end function   ''' <summary> ''' adds row target table. ''' </summary> ''' <param name="rowvalues">the row values want insert. these in order, presumed first row value in array first column  ''' of target data table.</param> ''' <remarks></remarks> public sub insertrow(byval paramarray rowvalues() string)      'if length of rowvalues not equal columns, means invalid insert. throw exception.     if rowvalues.length = dttargettable.columns.count          'creates new row.         dim drnewrow datarow         drnewrow = dttargettable.newrow          'goes through row values.         idx integer = 0 rowvalues.length - 1              'store value column.             drnewrow(dttargettable.columns(idx)) = rowvalues(idx)          next          'only adds key if primary key doesn't exist.         if dttargettable.rows.find(rowvalues(0)) nothing             'adds row table.             dttargettable.rows.insertat(drnewrow, 0)         end if      else         throw new exception(string.format("invalid insert. number of row values passed not equal number of columns of target datatable." & _                                           "the number of columns of target datatable {0}.", dttargettable.columns.count))     end if  end sub 

i have several suggestions should help:

don't retrieve keyname using index multiple times; using each loop reduces amount of processing.

don't swap xml files in each loop; instead, initialize instances outside of loop , pass them appropriate method (not sure instance type is, made 1 called xmlmanagerinstance).

don't check primary key existence in datatable on each loop. instead, keep list of used primary keys in outer loop , don't bother doing work if pk exists.

these should improve performance quite bit, last two.

here suggested reworking of code:

    dim keynames list(of string)      dim cprimarykeys new system.collections.generic.hashset(of string)     dim oenglishfile xmlmanagerinstance     dim ofrenchfile xmlmanagerinstance      oenglishfile.setreaderpath(dynamicproperties.instance.englishresourcepath)     ofrenchfile.setreaderpath(dynamicproperties.instance.frenchresourcepath)      each keyname string in keynames         if not cprimarykeys.contains(keyname)             cprimarykeys.add(keyname)             keymanagerresource.instance                 datatablemanager.instance.insertrow(keyname, .getkeyvalue(keyname, oenglishfile), .getkeyvalue(keyname, ofrenchfile))             end         end if     next  ''' <summary> ''' gets value of key. ''' </summary> ''' <param name="id">id of key.</param> ''' <returns>value of key.</returns> ''' <remarks></remarks> public function getkeyvalue(id string, fileinstance xmlmanagerinstance) string      dim returnednode xml.xmlnode = fileinstance.getnode(string.format("//data" & helper.caseinsensitivesearch("name"), "'" & id.tolower & "'"))      if returnednode isnot nothing         return returnednode.childnodes(1).innertext     else         return ""     end if  end function  ''' <summary> ''' adds row target table. ''' </summary> ''' <param name="rowvalues">the row values want insert. these in order, presumed first row value in array first column  ''' of target data table.</param> ''' <remarks></remarks> public sub insertrow(byval paramarray rowvalues() string)      'if length of rowvalues not equal columns, means invalid insert. throw exception.     if rowvalues.length = dttargettable.columns.count         'creates new row.         dim drnewrow datarow          drnewrow = dttargettable.newrow          'goes through row values.         idx integer = 0 rowvalues.length - 1             'store value column.             drnewrow(dttargettable.columns(idx)) = rowvalues(idx)         next     else         throw new exception(string.format("invalid insert. number of row values passed not equal number of columns of target datatable." & _                                           "the number of columns of target datatable {0}.", dttargettable.columns.count))     end if  end sub 

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -