python高斯求和_二、算法分析
一、什么是算法分析
程序和算法的區(qū)別:
算法是對(duì)問(wèn)題解決的分步描述
程序是采用某種編程語(yǔ)言實(shí)現(xiàn)的算法,同一個(gè)算法通過(guò)不同的程序員采用不同的編程語(yǔ)言,能產(chǎn)生很多程序
算法分析的概念:
算法分析主要就是從計(jì)算資源消耗的角度來(lái)評(píng)判和比較算法
更高效利用計(jì)算資源,或者更少占用計(jì)算資源的算法,就是好算法
計(jì)算資源指標(biāo):
一種是算法解決問(wèn)題過(guò)程中需要的存儲(chǔ)空間或內(nèi)存
另一種是算法的執(zhí)行時(shí)間
運(yùn)行時(shí)間檢測(cè):
1 #累計(jì)求和程序的運(yùn)行時(shí)間檢測(cè)
2 #迭代法
3 importtime4
5 defsum1(n):6 start =time.time()7 theSum =08 for i in range(1,n+1):9 theSum +=i10 end =time.time()11 return theSum, end -start12
13 for i in range(5):14 print('Sum is %d required %10.7f seconds'%sum1(10000))
n = 10000 時(shí),執(zhí)行5次的結(jié)果為
Sum is 50005000 required 0.0000000seconds
Sumis 50005000 required 0.0009980seconds
Sumis 50005000 required 0.0000000seconds
Sumis 50005000 required 0.0000000seconds
Sumis 50005000 required 0.0009968 seconds
n = 100000 時(shí), 執(zhí)行5次的結(jié)果為
Sum is 5000050000 required 0.0049877seconds
Sumis 5000050000 required 0.0049853seconds
Sumis 5000050000 required 0.0049860seconds
Sumis 5000050000 required 0.0049872seconds
Sumis 5000050000 required 0.0049863 seconds
n = 1000000時(shí),執(zhí)行5次的結(jié)果為
Sum is 500000500000 required 0.0528579seconds
Sumis 500000500000 required 0.0518231seconds
Sumis 500000500000 required 0.0528951seconds
Sumis 500000500000 required 0.0519009seconds
Sumis 500000500000 required 0.0527823 seconds
1 #累計(jì)求和程序的運(yùn)行時(shí)間檢測(cè)
2 #利用高斯求和公式的無(wú)迭代算法
3 importtime4
5 defsum2(n):6 start =time.time()7 theSum = (n * (n+1)) / 2
8 end =time.time()9 return theSum, end -start10
11 for i in range(5):12 print('Sum is %d required %10.7f seconds'%sum2(10000))
n = 10000,100000,1000000時(shí),執(zhí)行5次的結(jié)果時(shí)間均相同,為0.0000000 seconds
比較:
第一種迭代算法包含一個(gè)循環(huán),這個(gè)循環(huán)運(yùn)行次數(shù)跟累加值n有關(guān),n增加,循環(huán)次數(shù)也會(huì)增加
第二種無(wú)迭代算法運(yùn)行時(shí)間比第一種短很多,運(yùn)行時(shí)間與累計(jì)對(duì)象n的大小無(wú)關(guān)
二、大O表示法
算法時(shí)間度量指標(biāo):
一個(gè)算法所實(shí)施的操作數(shù)量或步驟數(shù)可作為獨(dú)立于具體程序/機(jī)器的度量指標(biāo)
賦值語(yǔ)句是度量指標(biāo)的一個(gè)合適選擇,一條賦值語(yǔ)句同時(shí)包含了(表達(dá)式)計(jì)算和(變量)存儲(chǔ)兩個(gè)基本資源
數(shù)量級(jí)函數(shù)(Order of Magnitude)
基本操作數(shù)量函數(shù)T(n)的精確值不是特別重要,重要的是T(n)中起決定性因素的主導(dǎo)部分
數(shù)量級(jí)函數(shù)描述了T(n)中隨著n增加而增加速度最快的主導(dǎo)部分
常見(jiàn)的大O數(shù)量級(jí)函數(shù):
三、"變位詞"判斷問(wèn)題
1 #解法1:逐字檢查
2
3 defanagramSolution1(s1, s2):4 alist =list(s2)5 pos1 =06 stillOK =True7 while pos1 < len(s1) andstillOK:8 pos2 =09 found =False10 while pos2 < len(alist) and notfound:11 if s1[pos1] ==s2[pos2]:12 found =True13 else:14 pos2 = pos2 + 1
15 iffound:16 alist[pos2] =None17 else:18 stillOK =False19 pos1 = pos1 + 1
20 returnstillOK21
22 print(anagramSolution1('abcd','cabd'))
1 #解法2:排序比較
2
3 defanagramSolution2(s1, s2):4 alist1 =list(s1)5 alist2 =list(s2)6
7 alist1.sort()8 alist2.sort()9 pos =010 matches =True11 while pos < len(s1) andmatches:12 if alist1[pos] ==alist2[pos]:13 pos = pos + 1
14 else:15 matches =False16 returnmatches17
18 print(anagramSolution2('abcde','edcba'))
1 #解法4:計(jì)數(shù)比較
2
3 defanagramSolution4(s1, s2):4 c1 = [0] * 26
5 c2 = [0] * 26
6 for i ins1:7 pos = ord(i) - ord('a')8 c1[pos] = c1[pos] + 1
9 for j ins2:10 pos = ord(j) - ord('a')11 c2[pos] = c2[pos] + 1
12 k =013 stillOK =True14 while k < 26 andstillOK:15 if c1[k] ==c2[k]:16 k = k + 1
17 else:18 stillOK =False19 returnstillOK20
21 print(anagramSolution4('abcde','edcba'))
四、Python數(shù)據(jù)類型的性能
1、列表list和字典dict的對(duì)比:
2、4種生成前n個(gè)整數(shù)列表的方法性能比較
1 #4種生成前n個(gè)整數(shù)列表的方法性能比較
2 from timeit importTimer3
4 deftest1():5 l =[]6 for i in range(1000):7 l = l +[i]8
9 deftest2():10 l =[]11 for i in range(1000):12 l.append(i)13
14 deftest3():15 l = [i for i in range(1000)]16
17 deftest4():18 l = list(range(1000))19
20 t1 = Timer('test1()','from __main__ import test1') #創(chuàng)建一個(gè)Timer對(duì)象,指定需要反復(fù)運(yùn)行的語(yǔ)句和只需要運(yùn)行一次的“安裝語(yǔ)句”
21 print('concat %f seconds\n' % t1.timeit(number=10000)) #調(diào)用Timer對(duì)象的timeit方法,其中可以指定反復(fù)運(yùn)行多少次
22
23 t2 = Timer('test2()','from __main__ import test2')24 print('append %f seconds\n' % t2.timeit(number=10000))25 t3 = Timer('test3()','from __main__ import test3')26 print('comprehension %f seconds\n' % t3.timeit(number=10000))27 t4 = Timer('test4()','from __main__ import test4')28 print('list range %f seconds\n' % t4.timeit(number=10000))
concat 14.517047seconds
append0.859504seconds
comprehension0.517713seconds
list range0.127913 seconds
3、list.pop的計(jì)時(shí)試驗(yàn)
#-*- coding: utf-8 -*-
"""Created on Thu Oct 17 20:30:27 2019
@author: wu"""
#list.pop的計(jì)時(shí)試驗(yàn),比較pop()和pop(0)兩種方法
from timeit importTimerimporttimeitimportnumpy as npimportmatplotlib.pyplot as plt
pop0= timeit.Timer('x.pop(0)','from __main__ import x')
popend= timeit.Timer('x.pop()','from __main__ import x')print('pop(0) pop()')
l1=[]
l2=[]for i in range(1000000,10000001,100000):
x=list(range(i))
pt= popend.timeit(number=100)
l1.append(pt)
x=list(range(i))
pz= pop0.timeit(number=100)
l2.append(pz)print('%15.5f,%15.5f' %(pz,pt))
j= np.arange(1000000,10000001,100000)
plt.plot(j,l1,label='pop')
plt.plot(j,l2,label='pop0')
plt.xlabel('n')
plt.ylabel('time(s)')
plt.legend()
plt.show()
4、list和dict的in操作對(duì)比
1 #驗(yàn)證list中檢索一個(gè)值,以及dict中檢索一個(gè)值的對(duì)比
2 importtimeit3 importrandom4 importnumpy as np5 importmatplotlib.pyplot as plt6
7 l1,l2 =[],[]8 n =[]9 for i in range(10000,1000001,20000):10 t = timeit.Timer('random.randrange(%d) in x'%i,'from __main__ import random,x')11 x =list(range(i))12 lst_time = t.timeit(number=100)13 l1.append(lst_time)14 x = {j:None for j inrange(i)}15 d_time = t.timeit(number=100)16 l2.append(d_time)17 print('{},{:10.3f},{:10.3f}'.format(i,lst_time,d_time))18
19 for i in range(10000,1000001,20000):20 n.append(i)21
22 plt.plot(n,l1,'go-',label='list')23 plt.plot(n,l2,'r*-',label='dict')24 plt.xlabel('n')25 plt.ylabel('time')26 plt.legend()27 plt.show()28
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的python高斯求和_二、算法分析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Spring的开幕式——Spring概述
- 下一篇: python中[-1]、[-1]、[-1