algorithm - Is it efficient to read, process and write one line at a time? -


i working on project requires reading file, making manipulations on each line , generate new file. bit concerned performance. algorithm more efficient? wrote pseudocode below.

  1. store array, close file, manipulate each line , store new array output file:

    openinputfile() linearray[] = readinput() closeinputfile()  (i in linearray) // i:current line     manipulate     newarray[] += // store manipulted line new array  openoutputfile() writeoutput(newarray) closeoutput() 
  2. get each line in loop, after manipulation write new line output

    openinputfile() openoutputfile()  (i in inputfile) // i:current line     manipulate     print manipulated line output  closeinputfile() closeoutputfile() 

which 1 should choose?

it depends on how large input file is:

  • if small, doesn't matter approach use.

  • if large enough, overhead of holding entire input file , entire output file in memory @ same time can have significant performance impacts. (increased paging load, etcetera.)

  • if large, run out of memory , application fail.

  • if cannot predict number of lines there be, preallocating line array problematic.


provided use buffered input , output streams, second version more efficient, use less memory, , won't break if input file big.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -