reset a memorystream
-
hey folks, what is the fastest way to have a clean fresh System.IO.MemoryStream each time i write into it? one way is to make a
ms.SetLength(0)
before i write in to it. Another way is to create a new one each time. i think this two methods aren't very fast. is there another posibility to clear a stream? thanks, bernd -
hey folks, what is the fastest way to have a clean fresh System.IO.MemoryStream each time i write into it? one way is to make a
ms.SetLength(0)
before i write in to it. Another way is to create a new one each time. i think this two methods aren't very fast. is there another posibility to clear a stream? thanks, bernd -
No, there isn't. If you want something faster, perhaps you should use something other than MemoryStream. Why the need for speed?
--- b { font-weight: normal; }
-
thanks guffa, speed is always nice to have ;-) and what's faster, setting the length to 0 or create a new one?
That depends. Setting the length to 0 should probably be faster than creating a new one, but the speed of that single action is not all that affects the speed of your application. A MemoryStream that is reset will probably handle memory allocations differently from a newly created one. I know too little about that difference, and especially too little about what your application is doing, to advice you in either direction. Don't do premature omtimization. You should always avoid anti-patterns (known bad solutions), but specific optimization should generally be put off until you know where and why you need it.
--- b { font-weight: normal; }
-
That depends. Setting the length to 0 should probably be faster than creating a new one, but the speed of that single action is not all that affects the speed of your application. A MemoryStream that is reset will probably handle memory allocations differently from a newly created one. I know too little about that difference, and especially too little about what your application is doing, to advice you in either direction. Don't do premature omtimization. You should always avoid anti-patterns (known bad solutions), but specific optimization should generally be put off until you know where and why you need it.
--- b { font-weight: normal; }