Re: BinaryWriter (or streams) slow performance
- From: "Ben Voigt" <rbv@xxxxxxxxxxxxx>
- Date: Tue, 27 Feb 2007 09:02:17 -0600
"Rock2000" <Rock2000@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:7AF22646-A323-42A0-8033-88FEEAC0FB51@xxxxxxxxxxxxxxxx
moduleI'm basically converting some Java code to C#, and for ths particular
I'm creating a file with a certain format from multiple input files.
The
format is certainly not pretty, but essentially the code is writing a
start
block, which contains a dummy int value for the block size, then it's
writing
the block data, and then seeking back up to fill in the block size
which it
now knows. Then seeking back to where it was to continue (yeah :(, I
didn't
design it). It does this for a ton of nested blocks. The problem is it
doesn't know the size of the block before writing it, and I don't want
to go
and redesign it.
Are the blocks small enough that you could create the block *and
header* in memory, and then write out the whole block in one go?
It varies because the blocks are nested. So it goes from a huge block that
basically surrounds the entire file, to smaller and smaller blocks that
nest
forever.
ASN.1 encodings?
One possible alternative, is to write out the whole file sequentially with
the placeholders, collecting the fixups in memory, and then make a second
pass doing the fixups. Also, try to do the fixups in sequential order (so
add to your collection when you write a placeholder, not when you calculate
the correct value). A LinkedList<long> ought to serve you well, let each
block have a LinkedListNode<long> which is created when you write the node
header, and filled in when you end the node and the length is known (you
could even store the stream.Location of the beginning of the data in that
node meanwhile, to make calculating the length easier). Then when you reach
the end of all your blocks, iterate over the entire list and write in the
corrected values.
.
- References:
- Re: BinaryWriter (or streams) slow performance
- From: Jon Skeet [C# MVP]
- Re: BinaryWriter (or streams) slow performance
- From: Jon Skeet [C# MVP]
- Re: BinaryWriter (or streams) slow performance
- Prev by Date: Re: BinaryWriter (or streams) slow performance
- Previous by thread: Re: BinaryWriter (or streams) slow performance
- Next by thread: Re: Windows only starts in safe mode
- Index(es):
Relevant Pages
|