def readPWM(pinNum): startpwm = timeit.timeit() highCount = 0 totCount = 0 cur = GPIO.input(pinNum) while True: prev = cur cur = GPIO.input(pinNum) if ( cur == 1 ): # The gpio pin is high, increment high count highCount += 1 elif ( prev == 1 and cur == 0 ): # Period has ended, now determine DC. dutyCycle = highCount/totCount else: totCount += 1 #End if #End while endpwm = timeit.timeit() totalTime = abs(1000*(endpwm - startpwm)) # Convert sec to ms print 'PWM took %.3f microseconds.' %(totalTime) return dutyCycle
def ujiKalangBersusuh(n): ls=[] jangkauan=range(1,n+1) siap = "from __main__ import kalangBersusuh" for i in jangkauan: print('i = ',i) t=timeit.timeit("kalangBersusuh(" +str(i) +")",setup=siap,number=1) ls.append(t) return ls
def test_timeit_comprehensions(): def for_loops(): numbers = list() for i in range(1000): if i % 2 != 0: numbers.append(i * 2) return numbers def comprehension(): return [i * 2 for i in range(1000) if i % 2 != 0] import timeit print( timeit.timeit(stmt='test()', number=10000, globals={'test': for_loops})) print( timeit.timeit(stmt='test()', number=10000, globals={'test': comprehension}))
def ujiKalangBersusuh(n): ls = [] jangkauan = range(1, n + 1) siap = "from __main__ import kalangBersusuh" for i in jangkauan: #print('i = ',i) #baris ini bisa dihidupkan atau dimatikan t = timeit.timeit("kalangBersusuh(" + str(i) + ")", setup=siap, number=1) ls.append(t) return ls
def timeit_test(): # 단순 append + pop은 list와 deque의 차이가 크지 않음 print( timeit.timeit(stmt='seq.append(0); seq.pop();', setup='seq=list(range(100));')) # 0.132167799 print( timeit.timeit( stmt='seq.append(0); seq.pop();', setup='from collections import deque; seq=deque(range(100));')) # 0.109020510 # insert를 통해 sec[0]에 값을 입력할 경우 큰 차이를 보임 print( timeit.timeit(stmt='seq.insert(0, 0); seq.pop();', setup='seq=list(range(10000))')) # 7.241453971 print( timeit.timeit( stmt='seq.appendleft(0); seq.pop();', setup='from collections import deque; seq=deque(range(10000));'))
assert (t == 0) # rule-id: use-current-time assert (t >= 0) # rule-id: use-current-time assert (t <= 0) # rule-id: use-current-time assert (t <> 0) # rule-id: use-current-time assert (t != 0) t = time.now() # rule-id: use-current-time assert (t > 0) # rule-id: use-current-time assert (t < 0) # rule-id: use-current-time assert (t == 0) # rule-id: use-current-time assert (t >= 0) # rule-id: use-current-time assert (t <= 0) # rule-id: use-current-time assert (t <> 0) # rule-id: use-current-time assert (t != 0) # rule-id: use-current-time time.clock() # rule-id: use-current-time a = timeit.timeit()
# C. Insertion Sort def insertionsort(a): for i in range(1, len(a)): nilai = a[i] b = i while b > 0 and nilai < a[b - 1]: a[b] = a[b-1] b -= 1 a[b] = nilai x = 1000 y = [3, 4, 2, 1, 2, 5, 9, 3] def jml1(): jumlahkan_cara_1(x) def jml2(): jumlahkan_cara_2(x) def inst(): insertionsort(y) if __name__ == '__main__': import timeit print("====================jumlahkan cara 1 timeit") print(timeit.timeit("jml1()", setup="from __main__ import jml1")) print("====================jumlahkan cara 2 timeit") print(timeit.timeit("jml2()", setup="from __main__ import jml2")) print("====================insertionSort timeit") print(timeit.timeit("inst()", setup="from __main__ import inst"))
#coding=utf8 #timeit from timeit import timeit as timeit ''' timeit.timeit(stmt='pass', setup='pass', timer=<default timer>, number=1000000) ''' #简短示例: print(timeit("math.sqrt(2.0)", "import math")) print(timeit("sqrt(2.0)", "from math import sqrt")) def test(): print 'tests' print(timeit("test()", "from __main__ import test", number=1000))