Comparison and Analysis of the Performance of Python judgment values in list or set; comparison of python Performance

Source: Internet
Author: User

Comparison and Analysis of the Performance of Python judgment values in list or set; comparison of python Performance

This article compares and analyzes whether Python judges whether the value is executed in the list or set. We will share this with you for your reference. The details are as follows:

The speed of determining whether a value is in the set is much faster than that of the list, because the hash is used for searching the set, and the time is at the O (1) level.

Assume that listA has elements. setA = set (listA) is the set after setA is converted to listA.
The following is a simple comparison:

For I in xrange (0, 5000000): if I in listA: passfor I in xrange (0, 5000000): if I in setA: pass

The first cycle uses 16 min, and the second cycle uses 52 s. It can be seen that it is much more efficient to determine whether a value exists in set.

Besides, converting from list to set does not take any time.

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.