Are ArrayLists more than twice as slow as arrays?

后端 未结 4 757
傲寒
傲寒 2021-01-02 02:06

I wrote a test that attempts to test two things:

  • Whether the size of a buffer array affects its performance, even if you don\'t use the whole buffer
  • T
4条回答
  •  北荒
    北荒 (楼主)
    2021-01-02 02:22

    Keep in mind that in using ArrayList, you are actually calling a function, which in the case of get() actually makes two other function calls. (One of which is a range check, which I suspect may be part of the delay).

    The important thing with ArrayList, is not so much how much faster or slower it is compared with straight arrays, but that it's access time always constant (like arrays). In the real world, you'll almost always find that the added delay is negligible. Especially if you have an application that even thinks about connecting to a database. :)

    In short, I think your test (and the results) are legit.

提交回复
热议问题