Uploaded on

 

More in: Business , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
304
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Promise make your python code faster by promising to behave yourself Ryan Kelly [email_address]
  • 2.
  • 5.
      The What
  • 6. items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): i = 0 total = 0 while i < len(items): total += calculate(items[i]) i += 1 return total
  • 7. items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): i = 0 total = 0 while i < len(items): total += calculate(items[i]) i += 1 return total timeit: 83
  • 8. items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): i = 0 total = 0 while i < len(items): total += calculate(items[i]) i += 1 return total
  • 9. items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): i = 0 total = 0 l_items = items while i < len( l_items ): total += calculate( l_items[i] ) i += 1 return total
  • 10. items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) @promise.invariant([“items”]) def aggregate(): i = 0 total = 0 while i < len( items ): total += calculate( items[i] ) i += 1 return total
  • 11. items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) @promise.invariant([“items”]) def aggregate(): i = 0 total = 0 while i < len( items ): total += calculate( items[i] ) i += 1 return total timeit: 83 73
  • 12. items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): i = 0 total = 0 while i < len(items): total += calculate(items[i]) i += 1 return total
  • 13. items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate( len=len ): i = 0 total = 0 while i < len(items): total += calculate(items[i]) i += 1 return total
  • 14. items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) @promise.constant([“len”]) def aggregate(): i = 0 total = 0 while i < len( items ): total += calculate( items[i] ) i += 1 return total
  • 15. items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) @promise.constant([“len”]) def aggregate(): i = 0 total = 0 while i < len( items ): total += calculate( items[i] ) i += 1 return total timeit: 83 77.1
  • 16. items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): i = 0 total = 0 while i < len(items): total += calculate(items[i]) i += 1 return total
  • 17. items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): i = 0 total = 0 while i < len(items): x = items[i] total += 3*x*x - 2*x + (1 / x) i += 1 return total
  • 18. items = [random.randint(0,100) for _ in xrange(100)] @promise.pure() def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): i = 0 total = 0 while i < len(items): total += calculate(items[i]) i += 1 return total
  • 19. items = [random.randint(0,100) for _ in xrange(100)] @promise.pure() def calculate(x): return 3*x*x - 2*x + (1 / x) @promise.constant([“calculate”]) def aggregate(): i = 0 total = 0 while i < len( items ): total += calculate( items[i] ) i += 1 return total
  • 20. items = [random.randint(0,100) for _ in xrange(100)] @promise.pure() def calculate(x): return 3*x*x - 2*x + (1 / x) @promise.constant([“calculate”]) def aggregate(): i = 0 total = 0 while i < len( items ): total += calculate( items[i] ) i += 1 return total timeit: 83 60.5
  • 21. items = [random.randint(0,100) for _ in xrange(100)] @promise.pure() def calculate(x): return 3*x*x - 2*x + (1 / x) @promise.invariant([“items”]) @promise.constant([“len”,“calculate”]) def aggregate(): i = 0 total = 0 while i < len( items ): total += calculate( items[i] ) i += 1 return total
  • 22. items = [random.randint(0,100) for _ in xrange(100)] @promise.pure() def calculate(x): return 3*x*x - 2*x + (1 / x) @promise.sensible() def aggregate(): i = 0 total = 0 while i < len( items ): total += calculate( items[i] ) i += 1 return total
  • 23. items = [random.randint(0,100) for _ in xrange(100)] @promise.pure() def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): i = 0 total = 0 while i < len( items ): total += calculate( items[i] ) i += 1 return total promise.sensible()(globals())
  • 24. items = [random.randint(0,100) for _ in xrange(100)] @promise.pure() def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): i = 0 total = 0 while i < len( items ): total += calculate( items[i] ) i += 1 return total promise.sensible()(globals()) timeit: 83 56.5
  • 25. items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): i = 0 total = 0 while i < len(items): total += calculate(items[i]) i += 1 return total
  • 26. items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): return sum(calculate(i) for i in items)
  • 27. items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): return sum(calculate(i) for i in items) timeit: 83 59
  • 28. items = [random.randint(0,100) for _ in xrange(100)] @promise.pure() def calculate(x): return 3*x*x - 2*x + (1 / x) @promise.sensible() def aggregate(): return sum(calculate(i) for i in items)
  • 29. items = [random.randint(0,100) for _ in xrange(100)] @promise.pure() def calculate(x): return 3*x*x - 2*x + (1 / x) @promise.sensible() def aggregate(): return sum([calculate(i) for i in items]) timeit: 83 42.6
  • 30.
      The How
  • 31. byteplay http://code.google.com/p/byteplay/ don't hack bytecode without it
  • 32. >>> a = 1 >>> def add_a(b): ... return a + b ... >>>
  • 33. >>> a = 1 >>> def add_a(b): ... return a + b ... >>> >>> >>> dis.dis(add_a) LOAD_GLOBAL 0 (a) LOAD_FAST 0 (b) BINARY_ADD RETURN_VALUE >>>
  • 34. >>> a = 1 >>> @promise.invariant([&quot;a&quot;]) ... def add_a(b): ... return a + b ... >>>
  • 35. >>> a = 1 >>> @promise.invariant([&quot;a&quot;]) ... def add_a(b): ... return a + b ... >>> >>> >>> dis.dis(add_a) LOAD_GLOBAL 0 (a) STORE_FAST 1 (_promise_var1_a) LOAD_FAST 1 (_promise_var1_a) LOAD_FAST 0 (b) BINARY_ADD RETURN_VALUE >>>
  • 36. >>> a = 1 >>> @promise.constant([&quot;a&quot;]) ... def add_a(b): ... return a + b ... >>>
  • 37. >>> a = 1 >>> @promise.constant([&quot;a&quot;]) ... def add_a(b): ... return a + b ... >>> >>> >>> dis.dis(add_a) LOAD_CONST 1 (1) LOAD_FAST 0 (b) BINARY_ADD
  • 38. >>> a = 1 >>> def calc(): ... return add_a(7) ... >>> def add_a(b): ... return a + b ... >>>
  • 39. >>> a = 1 >>> def calc(): ... return add_a(7) ... >>> def add_a(b): ... return a + b ... >>> >>> dis.dis(calc) LOAD_GLOBAL 0 (add_a) LOAD_CONST 1 (7) CALL_FUNCTION 1 RETURN_VALUE >>>
  • 40. >>> a = 1 >>> @promise.constant([&quot;add_a&quot;]) ... def calc(): ... return add_a(7) ... >>> @promise.pure() ... def add_a(b): ... return a + b ... >>>
  • 41. >>> a = 1 >>> @promise.constant([&quot;add_a&quot;]) ... def calc(): ... return add_a(7) ... >>> @promise.pure() ... def add_a(b): ... return a + b ... >>> >>> dis.dis(calc) LOAD_CONST 1 ( <function apply_deferred_promises> ) LOAD_CONST 2 (<function calc>) CALL_FUNCTION 1 POP_TOP LOAD_GLOBAL 0 (add_a) LOAD_CONST 3 (7) CALL_FUNCTION 1 RETURN_VALUE >>>
  • 42. >>> calc._promise_deferred [<promise.constant object at 0xb77a5b0c>] >>>
  • 43. >>> calc._promise_deferred [<promise.constant object at 0xb77a5b0c>] >>> >>> calc() 8 >>>
  • 44. >>> calc._promise_deferred [<promise.constant object at 0xb77a5b0c>] >>> >>> calc() 8 >>> >>> dis.dis(calc) LOAD_CONST 1 (7) STORE_FAST 0 (_promise_var3_b) LOAD_CONST 2 (1) LOAD_FAST 0 (_promise_var3_b) BINARY_ADD JUMP_ABSOLUTE 16 RETURN_VALUE >>>
  • 45.
      The Why
  • 46. For fun!
  • 47. For fun! None of my production code is CPU-bound
  • 48.
    • You probably don't need to optimise
  • 49.
    • You probably don't need to optimise
    • 50. You probably don't need to optimise that
  • 51.
    • You probably don't need to optimise
    • 52. You probably don't need to optimise that
    • 53. You probably need a better algorithm
  • 54.
    • Don't ugly up your code
  • 55. come fork me: http://github.com/rfk/promise/