Time complexity of the python infrastructure

Source: Internet
Author: User

Remember that the number of elements in the data structure is n

Listing (list)

The list is implemented by the array, and the allocated memory is a contiguous space. Adjust and modify the list elements to return the length of the list, the time complexity of these operations is O (1). The operation time in the head of the list is relatively high, O (n).

For example, in a personal local environment, 100,000 elements were added from the tail and head of the list, respectively, and the former took 10ms and the latter took 2100ms. In order to deal with this problem, Collections.deque can be used in particular. This double-ended queue is implemented by a doubly linked list, and the time complexity of deleting and inserting elements in the left and right two segments is O (1)

Dictionary (dict)

The basic implementation of a dictionary is mapped with a hash function. The time complexity of getting, deleting, adding Elements is O (1), the in operation is usually O (1), and the time complexity of O (n) occurs only when the hash function is set unreasonably and a large number of key-value mappings collide, and this is rarely the case.

Time complexity of the python infrastructure

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.