string assignment in vb.net (It's too slow
-
Hello :) I have a class : cls_Communication_base which has a private variable Private p_Descr as String = nothing and a public property public Overrides property [Descr]() as String get return me.p_Descr end get set (ByVal value As String) me.p_Descr = value end set end property I have some load method, which loads data from a data table and assigns the value from the datarow to the property row is a datarow from a datatable with 47000 Records public overrides sub fillDataRow(byval row as system.data.datarow,byval bolBuffer as boolean) ... If (Not row.isNull(5)) Then ' this line is too slow, really!!!!!!!!!! me.Descr = row(5).tostring() End If .... end sub Like said. Everything is ok in terms of speed. Only the line assigning the row value to the string property is really slow. OK. I am skipping a lot of lines here to make the example easy. But, Without this line assigning the value to the property (but creating 47000 instances of my class and doing everything like querying the database takes Zero Seconds). Just including this one line and the whole thing needs 72 seconds So, I guess there is something wrong in my way assigning strings to a property Is there a better way assigning values to a string property? Many thanks for helping :)
-
Hello :) I have a class : cls_Communication_base which has a private variable Private p_Descr as String = nothing and a public property public Overrides property [Descr]() as String get return me.p_Descr end get set (ByVal value As String) me.p_Descr = value end set end property I have some load method, which loads data from a data table and assigns the value from the datarow to the property row is a datarow from a datatable with 47000 Records public overrides sub fillDataRow(byval row as system.data.datarow,byval bolBuffer as boolean) ... If (Not row.isNull(5)) Then ' this line is too slow, really!!!!!!!!!! me.Descr = row(5).tostring() End If .... end sub Like said. Everything is ok in terms of speed. Only the line assigning the row value to the string property is really slow. OK. I am skipping a lot of lines here to make the example easy. But, Without this line assigning the value to the property (but creating 47000 instances of my class and doing everything like querying the database takes Zero Seconds). Just including this one line and the whole thing needs 72 seconds So, I guess there is something wrong in my way assigning strings to a property Is there a better way assigning values to a string property? Many thanks for helping :)
-
Hello :) I have a class : cls_Communication_base which has a private variable Private p_Descr as String = nothing and a public property public Overrides property [Descr]() as String get return me.p_Descr end get set (ByVal value As String) me.p_Descr = value end set end property I have some load method, which loads data from a data table and assigns the value from the datarow to the property row is a datarow from a datatable with 47000 Records public overrides sub fillDataRow(byval row as system.data.datarow,byval bolBuffer as boolean) ... If (Not row.isNull(5)) Then ' this line is too slow, really!!!!!!!!!! me.Descr = row(5).tostring() End If .... end sub Like said. Everything is ok in terms of speed. Only the line assigning the row value to the string property is really slow. OK. I am skipping a lot of lines here to make the example easy. But, Without this line assigning the value to the property (but creating 47000 instances of my class and doing everything like querying the database takes Zero Seconds). Just including this one line and the whole thing needs 72 seconds So, I guess there is something wrong in my way assigning strings to a property Is there a better way assigning values to a string property? Many thanks for helping :)
There is no other way to do this. Since creating a string involves memory allocation and filling in of data, remember, you're doing this 47,000 times! If your case, you're also looking up an array item then calling ToString() on that object. You're doing a lot of stuff to create a string, so the trick is to have the string already created for you, like in an XML document or something...
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Hello :) I have a class : cls_Communication_base which has a private variable Private p_Descr as String = nothing and a public property public Overrides property [Descr]() as String get return me.p_Descr end get set (ByVal value As String) me.p_Descr = value end set end property I have some load method, which loads data from a data table and assigns the value from the datarow to the property row is a datarow from a datatable with 47000 Records public overrides sub fillDataRow(byval row as system.data.datarow,byval bolBuffer as boolean) ... If (Not row.isNull(5)) Then ' this line is too slow, really!!!!!!!!!! me.Descr = row(5).tostring() End If .... end sub Like said. Everything is ok in terms of speed. Only the line assigning the row value to the string property is really slow. OK. I am skipping a lot of lines here to make the example easy. But, Without this line assigning the value to the property (but creating 47000 instances of my class and doing everything like querying the database takes Zero Seconds). Just including this one line and the whole thing needs 72 seconds So, I guess there is something wrong in my way assigning strings to a property Is there a better way assigning values to a string property? Many thanks for helping :)
I'd bet the problem is not in the string assignment but in the call to
tostring()
. Strings are objects with reference semantics. They are assigned by copying an address (think "pointer"). This is not going to be expensive. (Well, for a property it calls the setter method which simply does the simple assignment, and a good compiler will optimize this to eliminate the call overhead. You are doing timing in Release mode, not Debug, right?) This can be verified by removing the assignment but keepingrow(5).tostring()
and ignoring the returned string. The real questions are: What type of object doesrow(5)
return? How expensive is it'stostring()
implementation?