Visual C# Developer Center > Visual C# Forums > Visual C# General > Most efficient method of reading binary file
Ask a questionAsk a question
 

AnswerMost efficient method of reading binary file

  • Friday, November 06, 2009 7:41 PMjmgreen7 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have a binary file of known size that i am trying to parse and place into a treeview object.  I am trying to reduce the amount of time it takes to parse the data, create my objects, and display the data.  I did this using to different methods, and the results were surprising.  i'm wondering if i am doing something wrong here...
    Method 1:
    Read entire file into byte[]
    create new message instance and add to List
    continue until entire byte[] is read.
    iterate through list using for loop
    get msg instance
    add tree entry
    total time = 4371 miliseconds

    Method 2:
    read message into byte[]
    create new message instance
    add tree entry
    continue until entire byte array is read
    total time = 4098 milliseconds.

    Size of message = 2512640 bytes
    Number of records = 20381

    So if i am looping through a total of 40762 times in method 1, and 20381 times in method 2, how come my times are only marginally better?

Answers

  • Wednesday, November 11, 2009 10:23 AMMatthew Watson Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    If you are using Add() to add nodes, are you calling TreeView.BeginUpdate() before updating the TreeView, and TreeView.EndUpdate() afterwards? Otherwise a lot of time will be wasted redrawing the tree view.

    (The preferred method is to call AddRange() to add all the nodes at once; perhaps you're already doing that.)

All Replies

  • Friday, November 06, 2009 7:44 PMDavid M MortonMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    It's not the loop that's takes the bulk of the time.  It's reading the file.  I'd be willing to bet that nearly 3.5 seconds of this process is just reading the file itself.  After that, it's just gravy.
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
  • Friday, November 06, 2009 8:01 PMjmgreen7 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    actually, from what i see, the greatest amount of elapsed time is from adding the nodes to the treeview.
    My reading of hte file = 3 mil
    iterating through list to create msg instances = 78 mil
    getting the correct message based index and adding to tree:  4249 mil.
  • Wednesday, November 11, 2009 6:09 AMChao KuoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello, Jim Green
    David is right, reading file frequently will eat you too much time, I/O operations is really time consuming operation, because operating memory is much quicker than operating disk. So if you have enough memory, I recommend you to use your first method.
    If you feel it is too slow, I think you could do some optimization.
    Thanks
    Chao
  • Wednesday, November 11, 2009 10:23 AMMatthew Watson Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    If you are using Add() to add nodes, are you calling TreeView.BeginUpdate() before updating the TreeView, and TreeView.EndUpdate() afterwards? Otherwise a lot of time will be wasted redrawing the tree view.

    (The preferred method is to call AddRange() to add all the nodes at once; perhaps you're already doing that.)