fork download
  1. def google_merge(list1, list2):
  2. result = []
  3. while len(list1) and len(list2):
  4. if list1[0] < list2[0]:
  5. result.append(list1.pop(0))
  6. else:
  7. result.append(list2.pop(0))
  8. result.extend(list1)
  9. result.extend(list2)
  10. return result
  11.  
  12. def ewiethoff_merge(list1, list2):
  13. result = []
  14. while len(list1) and len(list2):
  15. if list1[-1] < list2[-1]:
  16. result.append(list2.pop())
  17. else:
  18. result.append(list1.pop())
  19. result.extend(reversed(list2))
  20. result.extend(reversed(list1))
  21. return list(reversed(result))
  22.  
  23. def list1(): return range(LO1, HI1)
  24. def list2(): return range(LO2, HI2)
  25.  
  26. LO1, LO2, HI1, HI2 = 2, 0, 9, 5
  27. print list1(), list2()
  28. print google_merge(list1(), list2())
  29. print ewiethoff_merge(list1(), list2())
  30. print; print
  31.  
  32. import cProfile
  33. LO1, LO2, HI1, HI2 = 202, 0, 90009, 50005
  34. cProfile.run('google_merge(list1(), list2())')
  35. cProfile.run('ewiethoff_merge(list1(), list2())')
  36.  
Success #stdin #stdout 4.87s 8216KB
stdin
Standard input is empty
stdout
[2, 3, 4, 5, 6, 7, 8] [0, 1, 2, 3, 4]
[0, 1, 2, 2, 3, 3, 4, 4, 5, 6, 7, 8]
[0, 1, 2, 2, 3, 3, 4, 4, 5, 6, 7, 8]


         399239 function calls in 4.306 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.003    0.003    4.306    4.306 <string>:1(<module>)
        1    0.328    0.328    4.298    4.298 prog.py:1(google_merge)
        1    0.000    0.000    0.003    0.003 prog.py:23(list1)
        1    0.000    0.000    0.002    0.002 prog.py:24(list2)
   199616    0.061    0.000    0.061    0.000 {len}
    99807    0.031    0.000    0.031    0.000 {method 'append' of 'list' objects}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        2    0.000    0.000    0.000    0.000 {method 'extend' of 'list' objects}
    99807    3.878    0.000    3.878    0.000 {method 'pop' of 'list' objects}
        2    0.005    0.003    0.005    0.003 {range}


         558446 function calls in 0.542 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.004    0.004    0.542    0.542 <string>:1(<module>)
        1    0.358    0.358    0.536    0.536 prog.py:12(ewiethoff_merge)
        1    0.000    0.000    0.002    0.002 prog.py:23(list1)
        1    0.000    0.000    0.001    0.001 prog.py:24(list2)
   279219    0.080    0.000    0.080    0.000 {len}
   139609    0.047    0.000    0.047    0.000 {method 'append' of 'list' objects}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        2    0.000    0.000    0.000    0.000 {method 'extend' of 'list' objects}
   139609    0.051    0.000    0.051    0.000 {method 'pop' of 'list' objects}
        2    0.003    0.001    0.003    0.001 {range}