MemoryStream Performance

J

JackRazz

Hi everyone,

I'm trying to figure out an efficient way to check a bunch of objects in a collection
to see if they have changed. They are all different types inherited from a base
class. One possiblility is to stream each object to memory and then compute a CRC
for that item. I can then compare to the original CRC. I profiled a routine to
stream them (see below), but it appears to be far too slow. The CRC computation time
was fine.

Does anyone see anything wrong with this code from a performance perspective. For
example, I could write the object directly to a memory stream with stream.Write(
buffer, offset, count ), but I don't know the object's size.

Any ideas - JackRazz


Public Shared Function ObjToMemStream(ByVal aControlSetting As
ControlSettingBase) As System.IO.MemoryStream
Dim stream As New System.IO.MemoryStream(256)
Dim bytes() As Byte
Dim bFormatter As New
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()

bFormatter.Serialize(stream, Obj)

End Function
 
N

Nicholas Paldino [.NET/C# MVP]

Jack,

Since they are all derived from one base class, I would add a property
to the base class that the object can set to indicate that it is dirty, or
has changed. Then, the collection class just has to cycle through the
objects, and check the property.

That would ultimately be much more performant than parsing the stream
and storing the CRC.

Hope this helps.
 
D

Dilip Krishnan

Exactly, Ideally the base can implement a property to keep tabs on changes.

e.g.

public class Base {
....
public bool Changed {
get {
return _changed
}
set {
_changed = value
}
}
}


public class Derived : Base {
.....
public int SomeProperty {
....
set {
_someProperty = value;
base.Changed = true;
}
}
}

Hope that helps. Code is in C# so you;d need to do the conversion
Thanks
 
J

JackRazz

Nicholas and Dilip,

Thanks for the info. I was simply trying to avoid checking every property to see if
its changed. I'm partway thru that (lots of classes) now. Anyhow, I was hoping that
there might be some problem with my memory stream code that slowed it down
drastically.

Thanks - JackRazz


| Exactly, Ideally the base can implement a property to keep tabs on changes.
|
| e.g.
|
| public class Base {
| ...
| public bool Changed {
| get {
| return _changed
| }
| set {
| _changed = value
| }
| }
| }
|
|
| public class Derived : Base {
| ....
| public int SomeProperty {
| ...
| set {
| _someProperty = value;
| base.Changed = true;
| }
| }
| }
|
| Hope that helps. Code is in C# so you;d need to do the conversion
| Thanks
|
|
| Nicholas Paldino [.NET/C# MVP] wrote:
|
| > Jack,
| >
| > Since they are all derived from one base class, I would add a property
| > to the base class that the object can set to indicate that it is dirty, or
| > has changed. Then, the collection class just has to cycle through the
| > objects, and check the property.
| >
| > That would ultimately be much more performant than parsing the stream
| > and storing the CRC.
| >
| > Hope this helps.
| >
| >
|
| --
| Regards,
| Dilip Krishnan
| MCAD, MCSD.net
| dilipdotnet at apdiya dot com
 
N

Nicholas Paldino [.NET/C# MVP]

JackRazz,

The fact that you have to:

a) Serialize the object to a stream
b) Loop through every byte in the stream

Is what is going to slow it down drastically. Your base object just has
to have a single property you can set when a property is changed. Then,
when your property is changed from the state that it should be in (where it
would be considered dirty), just have the code in the setter of the property
set the flag to true.

When it comes to check your collection of items, loop through the
collection, determining if any item has changed. If it has, act
appropriately.

You are going to save a lot of cycles by doing that.
 
J

Joerg Jooss

JackRazz said:
Hi everyone,

I'm trying to figure out an efficient way to check a bunch of objects
in a collection to see if they have changed. They are all different
types inherited from a base class. One possiblility is to stream
each object to memory and then compute a CRC for that item. I can
then compare to the original CRC. I profiled a routine to stream
them (see below), but it appears to be far too slow. The CRC
computation time was fine.

Does anyone see anything wrong with this code from a performance
perspective. For example, I could write the object directly to a
memory stream with stream.Write( buffer, offset, count ), but I don't
know the object's size.

What about using a MVC approach and implement an event in the base class
that communicates changes to all interested components?
 

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