Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. string assignment in vb.net (It's too slow

string assignment in vb.net (It's too slow

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpdatabaseperformancetutorial
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dl4gbe
    wrote on last edited by
    #1

    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 :)

    9 D M 3 Replies Last reply
    0
    • D dl4gbe

      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 :)

      9 Offline
      9 Offline
      9082365
      wrote on last edited by
      #2

      Not sure that there's any other way let alone a better one!

      1 Reply Last reply
      0
      • D dl4gbe

        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 :)

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • D dl4gbe

          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 :)

          M Offline
          M Offline
          Matt T Heffron
          wrote on last edited by
          #4

          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 keeping row(5).tostring() and ignoring the returned string. The real questions are: What type of object does row(5) return? How expensive is it's tostring() implementation?

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups