I’m using python 3.6
As the title suggests this code is about instantiating object attributes Just in Time.
I’m working on a REST API, where I currently create python objects from the JSON data returned by the server. Though not all attributes of the created objects will be called upon in user code.
Hence only instantiating the attributes the user access via dot notation seems like a valid way to improve performance.
lazy_evaluate is a function, with a signature similar to
lazy_evaluate returns a function that when called multiple times only computes the result once.
I then take advantage of the builtin
property function to create a getter that calls the function returned by
There is clearly a trade off here. This only helps performance if a subset of the attributes are required in user code. Otherwise the extra code will slow it down.
The core of the idea is to use python scoping to mutate a list in the scope outside the function
evaluate. I dislike this the most.
Do you think it’s a good idea? Do you have a better solution? Do you think the complexity is worth it?
This example runs a benchmark
from time import time class Time(object): def __enter__(self): self.start = time() return self def __exit__(self, exc_type, exc_val, exc_tb): self.end = time() self.interval = self.end - self.start def lazy_evaluate(func, *args, **kwargs): acc =  def evaluate(): if acc == : print('EVALUATING') acc.append(func(*args, **kwargs)) return acc return evaluate class JitAttributes(object): foo = property(lambda self: getattr(self, '_foo')()) bar = property(lambda self: getattr(self, '_bar')()) baz = property(lambda self: getattr(self, '_baz')()) def __init__(self, foo, bar, baz): self._foo = foo self._bar = bar self._baz = baz class Attributes(object): def __init__(self, foo, bar, baz): self.foo = foo self.bar = bar self.baz = baz a = 500 # Careful making this too big! b = a * 2 c = b * 2 print('RUNNING JitAttributes') repeats = range(100000) with Time() as t: for _ in repeats: JitAttributes( lazy_evaluate(list, range(a)), lazy_evaluate(list, range(b)), lazy_evaluate(list, range(c)), ) print('TOOK ', t.interval, ' seconds') print('RUNNING Attributes') with Time() as t: for _ in repeats: Attributes( list(range(a)), list(range(b)), list(range(c)), ) print('TOOK ', t.interval, ' seconds')
RUNNING JitAttributes TOOK 0.2956113815307617 seconds RUNNING Attributes TOOK 4.880501985549927 seconds
>>> jit_instance = JitAttributes( ... lazy_evaluate(list, range(a)), ... lazy_evaluate(list, range(b)), ... lazy_evaluate(list, range(c)), ... ) >>> jit_instance.foo # EVALUATING <- Only evaluates once :) # [0, 1, 2, 3, ... ] >>> jit_instance.foo # [0, 1, 2, 3, ... ] >>> non_jit_instance = Attributes( ... list(range(a)), ... list(range(b)), ... list(range(c)), ... ) >>> non_jit_instance.foo # [0, 1, 2, 3, ...] >>> non_jit_instance.foo # [0, 1, 2, 3, ...]
✓ Extra quality
ExtraProxies brings the best proxy quality for you with our private and reliable proxies
✓ Extra anonymity
Top level of anonymity and 100% safe proxies – this is what you get with every proxy package
✓ Extra speed
1,ooo mb/s proxy servers speed – we are way better than others – just enjoy our proxies!
USA proxy location
We offer premium quality USA private proxies – the most essential proxies you can ever want from USA
Our proxies have TOP level of anonymity + Elite quality, so you are always safe and secure with your proxies
Use your proxies as much as you want – we have no limits for data transfer and bandwidth, unlimited usage!
Superb fast proxy servers with 1,000 mb/s speed – sit back and enjoy your lightning fast private proxies!
99,9% servers uptime
Alive and working proxies all the time – we are taking care of our servers so you can use them without any problems
No usage restrictions
You have freedom to use your proxies with every software, browser or website you want without restrictions
Perfect for SEO
We are 100% friendly with all SEO tasks as well as internet marketing – feel the power with our proxies
Buy more proxies and get better price – we offer various proxy packages with great deals and discounts
We are working 24/7 to bring the best proxy experience for you – we are glad to help and assist you!