locked
What's the difference between setting Stream.Position and calling Stream.Seek method? RRS feed

  • Question

  • Hi All,

    I use FileStream in my class and I want to have best perfomance.
    So could you please tell me what I should prefer setting Stream.Position derectlly or using  Stream.Seek method.

    Thanks,

    Rosen Rusev
    Saturday, October 18, 2008 9:32 AM

Answers

  • You should measure, not guess.  Use System.Diagnostics.Stopwatch.  You'll find in this case that it takes exactly as long, the Position property setter uses Seek().  Optimizing file related code is generally a waste of time, it takes several orders of magnitude longer to read data off the disk than to manipulate that data.


    Hans Passant.
    • Edited by nobugz Saturday, October 18, 2008 1:30 PM ....
    • Marked as answer by rrossenbg Saturday, October 18, 2008 3:13 PM
    • Marked as answer by rrossenbg Saturday, October 18, 2008 3:13 PM
    • Marked as answer by rrossenbg Saturday, October 18, 2008 3:14 PM
    Saturday, October 18, 2008 1:26 PM
  • It's probably not where you think it is anyway, so it doesn't matter.  The OS typically caches 1024 bytes. 
    • Marked as answer by rrossenbg Saturday, October 18, 2008 3:13 PM
    Saturday, October 18, 2008 3:05 PM

All replies

  • You should measure, not guess.  Use System.Diagnostics.Stopwatch.  You'll find in this case that it takes exactly as long, the Position property setter uses Seek().  Optimizing file related code is generally a waste of time, it takes several orders of magnitude longer to read data off the disk than to manipulate that data.


    Hans Passant.
    • Edited by nobugz Saturday, October 18, 2008 1:30 PM ....
    • Marked as answer by rrossenbg Saturday, October 18, 2008 3:13 PM
    • Marked as answer by rrossenbg Saturday, October 18, 2008 3:13 PM
    • Marked as answer by rrossenbg Saturday, October 18, 2008 3:14 PM
    Saturday, October 18, 2008 1:26 PM
  • It's probably not where you think it is anyway, so it doesn't matter.  The OS typically caches 1024 bytes. 
    • Marked as answer by rrossenbg Saturday, October 18, 2008 3:13 PM
    Saturday, October 18, 2008 3:05 PM
  • Thanks both of you guys.
    • Edited by rrossenbg Saturday, October 18, 2008 3:29 PM
    Saturday, October 18, 2008 3:13 PM