為什麼要寫得更有效率 I

使用 pandas 撰寫高效程式碼

Leonidas Souliotis

PhD Researcher

怎麼量測時間?

time.time(): 回傳自 1970 年 1 月 1 日 12:00am 起的秒數

import time

# 執行前記錄時間 start_time = time.time()
# 執行運算 result = 5 + 2
# 執行後記錄時間 end_time = time.time()
print("Result calculated in {} sec".format(end_time - start_time))
Result calculated in 9.48905944824e-05 sec
使用 pandas 撰寫高效程式碼

for 迴圈 vs 串列生成式

  • 串列生成式:
    list_comp_start_time = time.time()
    result = [i*i for i in range(0,1000000)]
    list_comp_end_time = time.time()
    print("Time using the list_comprehension: {} sec".format(list_comp_end_time - 
    list_comp_start_time))
    
  • for 迴圈:
    for_loop_start_time= time.time()
    result=[]
    for i in range(0,1000000):
      result.append(i*i)
    for_loop_end_time= time.time()
    print("Time using the for loop: {} sec".format(for_loop_end_time - for_loop_start_time))
    
使用 pandas 撰寫高效程式碼

for 迴圈 vs 串列生成式 II

Time using the list comprehension: 0.11042404174804688 sec

Time using the for loop: 0.2071230411529541 sec
list_comp_time = list_comp_end_time - list_comp_start_time
for_loop_time = for_loop_end_time - for_loop_start_time
print("Difference in time: {} %".format((for_loop_time - list_comp_time)/
list_comp_time*100))
Difference in time: 87.55527367398622 %
使用 pandas 撰寫高效程式碼

時間在哪裡重要 I

計算 $1+2+...+1000000$。

  • 逐一相加:
def sum_brute_force(N):
    res = 0
    for i in range(1,N+1):
        res+=i
    return res
  • 使用 $\footnotesize 1 + 2 +... + N = \dfrac{N\cdot(N+1)}{2}$
def sum_formula(N):
    return N*(N+1)/2
使用 pandas 撰寫高效程式碼

時間在哪裡重要 II

  • 使用公式:
# Using the formula
formula_start_time = time.time()
formula_result = formula(1000000)
formula_end_time = time.time()

print("Time using the formula: {} 
sec".format(formula_end_time - formula_start_time))
Using the formula: 0.000108957290649 sec
  • 使用暴力法:
# Using brute force
bf_start_time = time.time()
bf_result = sum_brute_force(1000000)
bf_end_time = time.time()

print("Time using brute force: {} 
sec".format(bf_end_time - start_time))
Time using brute force: 0.174870967865 sec
Difference in speed: 160,394.967179%
使用 pandas 撰寫高效程式碼

開始動手!

使用 pandas 撰寫高效程式碼

Preparing Video For Download...