Python data structures and algorithms learn the second day of "time complexity and large o notation"

Source: Internet
Author: User


# !/usr/bin/env python
#! _*_ coding:utf-8 _*_ fromQueueImportQueueImportTimeque=Queue () Time_begin=time.time ()#if a+b+c=1000, and A^2+b^2=c^2,a,b,c is the natural number, find out all the a,b,c combinations#calculating results Using enumeration method forAinchRange (1001):     forBinchRange (1001):         forCinchRange (1001):            ifA + b + c = = 1000 anda**2 + b**2 = = C**2: Que.put ({'a'A'b': B,'C': C}) Time_end=time.time ()Print "The run time is%d and the result of the solution is as follows:"% (time_end-time_begin) forIteminchRange (Que.qsize ()):PrintQue.get ()

Results:

/users/liudaoqiang/pycharmprojects/numpy/venv/bin/python/users/liudaoqiang/project/python_project/bat_day1/abc.py run time is124, the result of the solution is as follows: {'a': 0,'C': 500,'b': 500}{'a': 200,'C': 425,'b': 375}{'a': 375,'C': 425,'b': 200}{'a': 500,'C': 500,'b': 0} Process finished with exit code 0

The same problem, the use of different algorithms, the running time is greatly reduced, as follows:

#!/usr/bin/env python#! _*_ coding:utf-8 _*_ fromQueueImportQueueImportTimeque=Queue () Time_begin=time.time ()#if a+b+c=1000, and A^2+b^2=c^2,a,b,c is the natural number, find out all the a,b,c combinations#calculating results Using enumeration method forAinchRange (1001):     forBinchRange (1001): C= 1000-a-bifa**2 + b**2 = = C**2: Que.put ({'a'A'b': B,'C': C}) Time_end=time.time ()Print "The run time is%d and the result of the solution is as follows:"% (time_end-time_begin) forIteminchRange (Que.qsize ()):PrintQue.get ()

Results:

/users/liudaoqiang/pycharmprojects/numpy/venv/bin/python/users/liudaoqiang/project/python_project/bat_day1/The abc2.py run time is 0, the result of the solution is as follows: {'a': 0,'C': 500,'b': 500}{'a': 200,'C': 425,'b': 375}{'a': 375,'C': 425,'b': 200}{'a': 500,'C': 500,'b': 0} Process finished with exit code 0

The same problem, found that the first algorithm with a time of 124S, the second method with less than 1S, which requires different algorithms to measure the operational efficiency;

How to measure efficiency? The running efficiency is not only related to the running time, but also to the running environment of the computer, the same algorithm is executed on different computers, and the execution time is not the same.

Therefore, the operational efficiency should be related to the execution steps, which will be the time complexity of the execution steps.

In the first algorithm: T (n) = n^3 * 2

In the second algorithm: T (n) = n^2 * 3

If the system and bias items are not considered, then the progressive function, using the progressive function representation, is the large O notation:

In the first algorithm: T (n) = O (n^3)

In the second algorithm: T (n) = O (n^2)

Python data structures and algorithms learn the second day of study "time complexity and large o notation"

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.