कुशल कोडिंग की ज़रूरत I

pandas के साथ कुशल कोड लिखना

Leonidas Souliotis

PhD Researcher

हम समय कैसे मापते हैं?

time.time(): 12:00am, January 1, 1970 से अब तक का समय सेकंड में लौटाता है

import time

# execution से पहले समय रिकॉर्ड करें start_time = time.time()
# ऑपरेशन चलाएँ result = 5 + 2
# execution के बाद समय रिकॉर्ड करें end_time = time.time()
print("Result calculated in {} sec".format(end_time - start_time))
Result calculated in 9.48905944824e-05 sec
pandas के साथ कुशल कोड लिखना

For loop बनाम List comprehension

  • List comprehension:
    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 loop:
    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 loop बनाम List comprehension 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...