Tuesday, November 8, 2016

C# performance notes


  • for v.s foreach
    1. for loops on List are a bit more than 2 times cheaper than foreach loops on List.
    2. Looping on Array is around 2 times cheaper than looping on List.
    3. As a consequence, looping on Array using for is 5 times cheaper than looping on List using foreach.
    4. LinkedList only allows foreach loops, as it would take quadratic time to loop a LinkedList through indices.
    5. Looping overheads: for < foreach; Array ~ List < ArrayList < LinkedList
  • Everything stored in an ArrayList is an object. So be careful of the boxing/unboxing overhead.
  • String concatenation overheads are negligible outside loops.