# !/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"