Promise
Upcoming SlideShare
Loading in...5
×
 

Promise

on

  • 574 views

 

Statistics

Views

Total Views
574
Views on SlideShare
574
Embed Views
0

Actions

Likes
1
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Promise Promise Presentation Transcript

    • Promise make your python code faster by promising to behave yourself Ryan Kelly [email_address]
      • The What
      • The How
      • The Why
      • The What
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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())
    • 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
    • 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
    • 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)
    • 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
    • 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)
    • 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
      • The How
    • 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_ADD RETURN_VALUE >>>
    • >>> 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) LOAD_GLOBAL 0 (a) STORE_FAST 1 (_promise_var1_a) LOAD_FAST 1 (_promise_var1_a) LOAD_FAST 0 (b) BINARY_ADD RETURN_VALUE >>>
    • >>> 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) LOAD_CONST 1 (1) LOAD_FAST 0 (b) BINARY_ADD
    • >>> 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_GLOBAL 0 (add_a) LOAD_CONST 1 (7) CALL_FUNCTION 1 RETURN_VALUE >>>
    • >>> a = 1 >>> @promise.constant([&quot;add_a&quot;]) ... def calc(): ... return add_a(7) ... >>> @promise.pure() ... def add_a(b): ... return a + b ... >>>
    • >>> 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 >>>
    • >>> 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 (7) STORE_FAST 0 (_promise_var3_b) LOAD_CONST 2 (1) LOAD_FAST 0 (_promise_var3_b) BINARY_ADD JUMP_ABSOLUTE 16 RETURN_VALUE >>>
      • The Why
    • For fun!
    • For fun! None of my production code is CPU-bound
      • You probably don't need to optimise
      • You probably don't need to optimise
      • You probably don't need to optimise that
      • You probably don't need to optimise
      • You probably don't need to optimise that
      • You probably need a better algorithm
      • Don't ugly up your code
    • come fork me: http://github.com/rfk/promise/