I have a method that reads from values from a file (reading method) and call another method from a Data Access Object (DAO) class which store these values into DB (storing method). My question is: shall i pass all the values read in the reading method as a list to the storing method ( which saves hundreds of method calls, but introduce the need for creation of the list in the reading method iteration over the list in the storing method), or shall i make a separate call to the storing method for each value ( which means hundreds of method calls, but without the need for list creation and iteration). which approach is more efficient from a performance and good practice view of point?
Share
You have multiple values, so someone has to iterate over the values. The only question is: who?
Generally speaking: As soon as you talk about DB access (reading and writing) the overhead of allocating a list, iterating over it and doing some method calls is negligible to the overhead of one DB call (we talk about hundreds of call, not billions, right?). A DB call is in most cases something like a remote procedure call and writing to a database will write to disk – another great performance sucker.
So in most cases you gain performance if you minimize the calls to the DB. And that can be done more easily if you DB access layer knows the complete job. So if you give the DAO a list it can do stuff like prepared statement, batch updates and what not. Given only bits and pieces those things are impossible to do with good performance.