Sometimes the only way things ever got fixed is because people became uncomfortable.
Stephan van Hulst wrote:because the capacity doubles every time. This logarithm cancels out with the linear cost of copying the elements, and so over many calls to ArrayList.add(), the average time becomes O(1).
Sometimes the only way things ever got fixed is because people became uncomfortable.
Asif Haciyev wrote:but as a entire time complexity we have to take worst case time. Is it true? Did I understand right?
But I didn't get just this sentence. Can you explain please?
Stephan van Hulst wrote:because the capacity doubles every time. This logarithm cancels out with the linear cost of copying the elements, and so over many calls to ArrayList.add(), the average time becomes O(1).
Tim Holloway wrote:What you code isn't always what you get, even at the higher levels. I've already mentioned that we've gone beyond the determinate to the statistical at the machine level.
Most compilers these days have some pretty extensive optimizations. For example, there's loop unrolling:
Campbell Ritchie wrote:And remember that a plain simple if doesn't exist in CS terms; you must always impute an else to it, even if that else is empty.
Sometimes the only way things ever got fixed is because people became uncomfortable.
Tim Holloway wrote:all a switch statement is is just a bunch of implied "IF" statements that happen to be predicated on mutually-exclusive conditions.
Liutauras Vilda wrote:
Tim Holloway wrote:all a switch statement is is just a bunch of implied "IF" statements that happen to be predicated on mutually-exclusive conditions.
Not sure I understood you correctly, but that's not true. Perhaps that's true in situations with really few switch cases, but when the number grows switch statements use lookup tables. So it outperforms technically bunch of if-else statements as search of matching condition is O(1) as oppose to bunch of if-then-else goes in a linear fashion.
Sometimes the only way things ever got fixed is because people became uncomfortable.
Sometimes the only way things ever got fixed is because people became uncomfortable.
Tim Holloway wrote:the worst-case performance would be O(n+1) where n is the number of cases tested plus optionally a default.
The constant factor is low compared to that for the LinkedList implementation.
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
|