Which DataContext method will be faster?
I have using a basic DataContext to create objects then submit these into a database.
Have written a couple of tests myself to see which is fast but just wondering which method is considered best practice out of the following.
Code iterates through a loop and instantiates an object which is to be persisted to the database. Is it better to:
1.) Create a list of objects then and assign each created object to the list then at the end use
MyDataContext.InsertAllOnSubmit(ListOfObjects)
2.) Assign each created object directly into the DataContext using
MyDataContext.InsertOnSubmit(Object)开发者_如何学JAVA
Hope this makes sense, if anyone needs more information let me know!
Thanks
I assume we're talking about the performance impact on the submit event - there is no database connection opened immediately when these methods are called.
Since each implementation will only update the database on Submit, they are both very similar. Any performance difference will be marginal (and will be countered by whatever processing you do to put the objects into the List or enumerate the List), so go with whichever fits better into your design.
You might find this page about premature optimization interesting - http://c2.com/cgi/wiki?PrematureOptimization
Premature optimization is the root of all evil -- Donald Knuth.
I guess for the second option, you'll need to re-open the connection for each operation. Using a list is cleaner and a better option.
精彩评论