By Python be understood that the time complexity of the program, the method used is: a few weight for the test loop, while in only a time complexity of O (n-) , double compared to O (n-2 ^) , and so on, if there is half compared with O (logN) , for example, half of the rapid power , binary search, if a for loop sets a half, then was the time complexity O (nlogn) .
By ascending order of magnitude, common time complexity are:
Constant order O (1), of the order of O ( L og2n ), linear order O (n),
Linear order of O (nlog2n), the square of the order O (n-2 ^) , the cubic order O (n-^. 3), ... ,
k th order O (n ^ k), exponential order O (n-2 ^) . As the problem size n is increasing the time complexity is increasing, the lower the efficiency of the algorithm.
The following are examples of actual test: three for a heavy cycle, three cycles for double
First, for a re-circulating
1. A loop for re - calculation 1,2,4,8,16 million times the number of seconds required for
2. double for the cycle - operation 1,2,4,8,16 one thousand times the square of the number of seconds required
import time
pr=1000
print("%s%10s" % ("problemSize","secondsA"))
for count in range(5):
start=time.time()
work=1
for i in range(pr):
for x in range(pr):
work+=1
en=time.time()-start
print("%5d平方次%10.5f" % (pr,en))
pr*=2