魔术优化 [英] Magic Optimisation

查看:71
本文介绍了魔术优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello People。


我有一个非常紧的内循环(在游戏应用程序中,所以每毫秒

计数)我有优化如下:


def loop(self):

self_pool = self.pool

self_call_exit_funcs = self.call_exit_funcs

self_pool_popleft = self.pool.popleft

self_pool_append = self.pool.append

check = self.pool .__ len__

而check()> 0:

task = self_pool_popleft()

尝试:

task.next()

除了StopIteration:

self_call_exit_funcs(任务)

返回

self_pool_append(任务)


这种优化方式已削减我的迭代中的_seconds_

循环,尤其是当我有很多注册任务时,所以这种风格的优化对我来说非常重要。


然而,它非常难看。有没有人有任何关于如何获得的建议

通过装饰师神奇地对这个优化进行优化?


Sw。

解决方案

这不是更漂亮,但是如果你从while循环中提取try-except

开销呢?你只希望在列表末尾点燃一次..... / b
的例外。对于未在循环中调用的调用,还可以消除任何

变量的本地化,

,例如self_pool(似乎根本不使用),以及

self_call_exit_funcs。


- Paul


def loop(self):

self_pool_popleft = self.pool.popleft

self_pool_append = self.pool.append

check = self.pool .__ len__

尝试:

而check()> 0:

task = self_pool_popleft()

task.next()

self_pool_append(任务)

除了StopIteration :

self.call_exit_funcs(任务)

返回


si ********** @ gmail.com 写道:

但是,它是十分难看。有没有人有任何关于如何通过装饰师神奇地对这个优化进行优化的提示?




你试过psyco吗?


我想在没有完整的

示例的情况下很难看到代码在做什么。


StopIteration实际上是由task.next()引发的,此时

任务从生成器列表中删除(self.pool)。所以

可以随时提高StopIteration。


我所追求的具体优化,这将清理代码

很多,是一种从self.attribute实例变量自动神奇地创建self_attribute局部变量

的方法。


Sw。


Hello People.

I''ve have a very tight inner loop (in a game app, so every millisecond
counts) which I have optimised below:

def loop(self):
self_pool = self.pool
self_call_exit_funcs = self.call_exit_funcs
self_pool_popleft = self.pool.popleft
self_pool_append = self.pool.append
check = self.pool.__len__
while check() > 0:
task = self_pool_popleft()
try:
task.next()
except StopIteration:
self_call_exit_funcs(task)
return
self_pool_append(task)

This style of optimisation has shaved _seconds_ from my iteration
cycle, esp. when I have many registered tasks, so this style of
optimisation is very important to me.

However, it is very ugly. Does anyone have any tips on how I could get
this optimisation to occor magically, via a decorator perhaps?

Sw.

解决方案

This isn''t much prettier, but what if you extract the try-except
overhead out from the while loop? You only expect the exception to
fire one time, at the end of the list. You can also eliminate any
localization of variables for calls that are not called in the loop,
such as self_pool (which does not seem to be used at all), and
self_call_exit_funcs.

-- Paul

def loop(self):
self_pool_popleft = self.pool.popleft
self_pool_append = self.pool.append
check = self.pool.__len__
try:
while check() > 0:
task = self_pool_popleft()
task.next()
self_pool_append(task)
except StopIteration:
self.call_exit_funcs(task)
return


si**********@gmail.com writes:

However, it is very ugly. Does anyone have any tips on how I could get
this optimisation to occor magically, via a decorator perhaps?



Have you tried psyco?


I guess it is hard to see what the code is doing without a complete
example.

The StopIteration is actually raised by task.next(), at which point
task is removed from the list of generators (self.pool). So the
StopIteration can be raised at any time.

The specific optimisation I am after, which will clean up the code a
lot, is a way to auto-magically create self_attribute local variables
from self.attribute instance variables.

Sw.


这篇关于魔术优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆