String or Integer?


G

Guest

Hi

Im a relative newbie to the .NET CF and am writing in VB.NET

I have a class (class A) that could hold a potential of 15 different classes in its "data" member field. I have added a field called "dataobjectname" which I set whenever I add the data member. Whenever I look to call the "convertToBytes" of on class A I read the "dataobjectname" and cast the object accordingly and call its "convertToBytes" member method

I looking to optimise my code and thought that Id replace the field "String - dataobjectname" with "Int16 - dataobjectname" and use an enumeration list" before casting.

Will this reduce the amount of memory used in my application? Will there be performance improvements

E.

Class
..
..
Dim data As Object ' Can hold any 1 of 15 different objects
Dim dataobjectname As String

Im open to suggesting in case anyone knows of how to do away with the dataobjectname field altogether so that I save even more memory whilst my app is running
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

Trevor said:
Im open to suggesting in case anyone knows of how to do away with the
dataobjectname field altogether so that I save even more memory
whilst my app is running.

I would suggest calling GetType on the object, myself.
 
C

Chris Tacke, eMVP

As Jon suggests, the objects already carry their type information with them,
so you can call getType on the object itself. There's no point in carrying
a separate 'type' field.

-Chris


Trevor said:
Hi,

Im a relative newbie to the .NET CF and am writing in VB.NET.

I have a class (class A) that could hold a potential of 15 different
classes in its "data" member field. I have added a field called
"dataobjectname" which I set whenever I add the data member. Whenever I
look to call the "convertToBytes" of on class A I read the "dataobjectname"
and cast the object accordingly and call its "convertToBytes" member method.
I looking to optimise my code and thought that Id replace the field
"String - dataobjectname" with "Int16 - dataobjectname" and use an
enumeration list" before casting.
Will this reduce the amount of memory used in my application? Will there be performance improvements?

E.g

Class A
...
...
Dim data As Object ' Can hold any 1 of 15 different objects.
Dim dataobjectname As String

Im open to suggesting in case anyone knows of how to do away with the
dataobjectname field altogether so that I save even more memory whilst my
app is running.
 
G

Guest

Thanks

Just out of interest... how much memory does a String hold? If not initialised

Thanks.
 
C

Chris Tacke, eMVP

Not sure, but intuitively it's got to be probably at least 8 bytes. A
length and a data pointer address. It's probably more as it carries other
info with it (like the type).
 
G

Guest

Thanks

This may sound a little strange but... if I did a check against a integer value, e.

if in16 = X the

would this be faster compared to

if dataobject.getType() = "value" the

Reason why im asking is that Im looking to squeaze as much performance out of my application as possible

Thanks.
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

Chris Tacke said:
Not sure, but intuitively it's got to be probably at least 8 bytes. A
length and a data pointer address. It's probably more as it carries other
info with it (like the type).

String is a bit strange. The object itself is variable length (I
believe) - the character data is stored within the object rather than
there being a pointer to a character array (as you get in Java, for
instance). IIRC, the size is something like 16 + 2 bytes per character,
including a null terminator (for interop reasons), rounded up to a
multiple of 4 bytes.
 
C

Chris Tacke, eMVP

Actually that makes sense as string manipulation is so slow. It must
reallocate and move the entire object during concatenation operations. 16 +
2*chars sounds about right.

-Chris
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

Actually that makes sense as string manipulation is so slow. It must
reallocate and move the entire object during concatenation operations. 16 +
2*chars sounds about right.

Just remembered one other thing - the actual size will also depend on
how it's been created. If you construct a string using StringBuilder,
there may well be extra unused buffer space which is part of the string
object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top