Promise make your python code faster by promising to behave yourself Ryan Kelly [email_address]
<ul><li>The What
The How
The Why </li></ul>
<ul>The What </ul>
items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): i = 0...
items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): i = 0...
items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): i = 0...
items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): i = 0...
items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) @promise.invariant([“i...
items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) @promise.invariant([“i...
items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): i = 0...
items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate( len=len...
items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) @promise.constant([“le...
items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) @promise.constant([“le...
items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): i = 0...
items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): i = 0...
items = [random.randint(0,100) for _ in xrange(100)] @promise.pure() def calculate(x): return 3*x*x - 2*x + (1 / x) def ag...
items = [random.randint(0,100) for _ in xrange(100)] @promise.pure() def calculate(x): return 3*x*x - 2*x + (1 / x) @promi...
items = [random.randint(0,100) for _ in xrange(100)] @promise.pure() def calculate(x): return 3*x*x - 2*x + (1 / x) @promi...
items = [random.randint(0,100) for _ in xrange(100)] @promise.pure() def calculate(x): return 3*x*x - 2*x + (1 / x) @promi...
items = [random.randint(0,100) for _ in xrange(100)] @promise.pure() def calculate(x): return 3*x*x - 2*x + (1 / x) @promi...
items = [random.randint(0,100) for _ in xrange(100)] @promise.pure() def calculate(x): return 3*x*x - 2*x + (1 / x) def ag...
items = [random.randint(0,100) for _ in xrange(100)] @promise.pure() def calculate(x): return 3*x*x - 2*x + (1 / x) def ag...
items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): i = 0...
items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): retur...
items = [random.randint(0,100) for _ in xrange(100)] def calculate(x): return 3*x*x - 2*x + (1 / x) def aggregate(): retur...
items = [random.randint(0,100) for _ in xrange(100)] @promise.pure() def calculate(x): return 3*x*x - 2*x + (1 / x) @promi...
items = [random.randint(0,100) for _ in xrange(100)] @promise.pure() def calculate(x): return 3*x*x - 2*x + (1 / x) @promi...
<ul>The How </ul>
byteplay http://code.google.com/p/byteplay/ don't hack bytecode without it
>>> a = 1 >>> def add_a(b): ...  return a + b ...  >>>
>>> a = 1 >>> def add_a(b): ...  return a + b ...  >>>  >>> >>> dis.dis(add_a) LOAD_GLOBAL  0 (a) LOAD_FAST  0 (b) BINARY_...
>>> a = 1 >>> @promise.invariant([&quot;a&quot;]) ... def add_a(b): ...  return a + b ...  >>>
>>> a = 1 >>> @promise.invariant([&quot;a&quot;]) ... def add_a(b): ...  return a + b ...  >>>  >>>  >>> dis.dis(add_a) LO...
>>> a = 1 >>> @promise.constant([&quot;a&quot;]) ... def add_a(b): ...  return a + b ...  >>>
>>> a = 1 >>> @promise.constant([&quot;a&quot;]) ... def add_a(b): ...  return a + b ...  >>>  >>>  >>> dis.dis(add_a) LOA...
>>> a = 1 >>> def calc(): ...  return add_a(7) ...  >>> def add_a(b): ...  return a + b ...  >>>
>>> a = 1 >>> def calc(): ...  return add_a(7) ...  >>> def add_a(b): ...  return a + b ...  >>>  >>> dis.dis(calc) LOAD_G...
>>> a = 1 >>> @promise.constant([&quot;add_a&quot;]) ... def calc(): ...  return add_a(7) ...  >>> @promise.pure() ... def...
>>> a = 1 >>> @promise.constant([&quot;add_a&quot;]) ... def calc(): ...  return add_a(7) ...  >>> @promise.pure() ... def...
>>> calc._promise_deferred [<promise.constant object at 0xb77a5b0c>] >>>
>>> calc._promise_deferred [<promise.constant object at 0xb77a5b0c>] >>> >>> calc() 8 >>>
>>> calc._promise_deferred [<promise.constant object at 0xb77a5b0c>] >>> >>> calc() 8 >>>  >>> dis.dis(calc) LOAD_CONST  1...
<ul>The Why </ul>
For fun!
For fun! None of my production code is CPU-bound
<ul><li>You probably don't need to optimise </li></ul>
<ul><li>You probably don't need to optimise
You probably don't need to optimise  that </li></ul>
Upcoming SlideShare
Loading in...5
×

Promise

331

Published on

Published in: Business, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
331
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Promise

  1. 1. Promise make your python code faster by promising to behave yourself Ryan Kelly [email_address]
  2. 2. <ul><li>The What
  3. 3. The How
  4. 4. The Why </li></ul>
  5. 5. <ul>The What </ul>
  6. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 30. <ul>The How </ul>
  31. 31. byteplay http://code.google.com/p/byteplay/ don't hack bytecode without it
  32. 32. >>> a = 1 >>> def add_a(b): ... return a + b ... >>>
  33. 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. 34. >>> a = 1 >>> @promise.invariant([&quot;a&quot;]) ... def add_a(b): ... return a + b ... >>>
  35. 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. 36. >>> a = 1 >>> @promise.constant([&quot;a&quot;]) ... def add_a(b): ... return a + b ... >>>
  37. 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. 38. >>> a = 1 >>> def calc(): ... return add_a(7) ... >>> def add_a(b): ... return a + b ... >>>
  39. 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. 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. 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. 42. >>> calc._promise_deferred [<promise.constant object at 0xb77a5b0c>] >>>
  43. 43. >>> calc._promise_deferred [<promise.constant object at 0xb77a5b0c>] >>> >>> calc() 8 >>>
  44. 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. 45. <ul>The Why </ul>
  46. 46. For fun!
  47. 47. For fun! None of my production code is CPU-bound
  48. 48. <ul><li>You probably don't need to optimise </li></ul>
  49. 49. <ul><li>You probably don't need to optimise
  50. 50. You probably don't need to optimise that </li></ul>
  51. 51. <ul><li>You probably don't need to optimise
  52. 52. You probably don't need to optimise that
  53. 53. You probably need a better algorithm </li></ul>
  54. 54. <ul><li>Don't ugly up your code </li></ul>
  55. 55. come fork me: http://github.com/rfk/promise/
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×