Upgrade to Pro — share decks privately, control downloads, hide ads and more …

計算名字 list 中開頭字母的人數

計算名字 list 中開頭字母的人數

所謂小題大作就是這個概念?我真的不知道簡介要寫什麼。

如果你真的想知道為什麼 .get 很慢,請看 http://www.slideshare.net/ya790026/why-is-a1-fast-than-aget1

Tzu-ping Chung

March 26, 2015
Tweet

More Decks by Tzu-ping Chung

Other Decks in Programming

Transcript

  1. Python 尝剣 switch

    View full-size slide

  2. Guido: 欽 dict

    View full-size slide

  3. counts  =  {}  
    for  name  in  namelist:  
           if  name[0]  not  in  counts:  
                   counts[name[0]]  =  1  
           else:  
                   counts[name[0]]  +=  1  
    for  key  in  counts:  
           count  =  counts[key]

    View full-size slide

  4. LBYL v. EAFP

    View full-size slide

  5. Easier to ask for forgiveness
    than permission.

    View full-size slide

  6. counts  =  {}  
    for  name  in  namelist:  
           try:  
                   counts[name[0]]  +=  1  
           except  KeyError:  
                   counts[name[0]]  =  1  
    for  key  in  counts:  
           count  =  counts[key]

    View full-size slide

  7. counts  =  {}  
    for  name  in  namelist:  
           val  =  counts.get(name[0],  0)  
           counts[name[0]]  =  val  +  1  
    for  key  in  counts:  
           count  =  counts[key]

    View full-size slide

  8. from  collections  import  defaultdict  
    counts  =  defaultdict(lambda:  0)  
    for  name  in  namelist:  
           counts[name[0]]  +=  1  
    for  key  in  counts:  
           count  =  counts[key]

    View full-size slide

  9. from  collections  import  Counter  
    counts  =  Counter(  
           name[0]  for  name  in  namelist  
    )  
    for  key  in  counts:  
           count  =  counts[key]

    View full-size slide

  10. from  itertools  import  groupby  
    for  k,  v  in  groupby(  
                   sorted(namelist),    
                   key=lambda  n:  n[0]):  
           count  =  len(list(v))

    View full-size slide

  11. from  itertools  import  groupby  
    for  k,  v  in  groupby(  
                   sorted(namelist),    
                   key=lambda  n:  n[0]):  
           count  =  len(list(v))
    馄䌃

    View full-size slide

  12. counts  =  {}  
    for  name  in  namelist:  
           if  name[0]  not  in  counts:  
                   counts[name[0]]  =  1  
           else:  
                   counts[name[0]]  +=  1  
    for  key  in  counts:  
           count  =  counts[key]
    Loop

    View full-size slide

  13. for  k,  v  in  groupby(  
                   sorted(namelist),    
                   key=lambda  n:  n[0]):  
           count  =  len(list(v))
    Loop
    Implicit loop

    View full-size slide

  14. Let’s Measure!

    View full-size slide

  15. • name = str(random.random())[2:]
    • Exclude import overhead
    • Benchmarking against dict + LBYL

    View full-size slide

  16. 侶ꆀ䖍㢳Counter 䩝剙㥨欽

    View full-size slide

  17. ⡎〪銲欽ⵋ EAFP 莃
    Ⰹ䒉䊨Ⱗ穠卓鿪䊴♶㢳

    View full-size slide

  18. EAFP ⮚倳 LBYL

    View full-size slide

  19. .get 㥨䢧

    View full-size slide

  20. • try is cheap, except is (more)
    expensive
    • If you hit often, use try-except
    • If you fail often, use .get

    View full-size slide

  21. Use the source, Luke!

    View full-size slide

  22. .__getitem__
    • Check argument type
    (string optimisation)
    • Value lookup
    • Return value on
    success
    • Extra checks and
    KeyError on failure
    .get
    • Argument unpacking

    • Value lookup
    • Return the value on
    success
    • Return default value on
    failure

    View full-size slide

  23. .__getitem__
    • Check argument type
    (string optimisation)
    • Value lookup
    • Return value on
    success
    • Extra checks and
    KeyError on failure
    .get
    • Argument unpacking

    • Value lookup
    • Return the value on
    success
    • Return default value on
    failure
    Super slow!
    Super fast!

    View full-size slide

  24. • .get is generally much slower,
    due to argument unpacking
    • If you fail often, .get’s default
    value might make up speed

    View full-size slide

  25. Python function calls
    are slow.

    View full-size slide