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_pople ft = self.pool.pople ft
self_pool_appen d = self.pool.appen d
check = self.pool.__len __
while check() > 0:
task = self_pool_pople ft()
try:
task.next()
except StopIteration:
self_call_exit_ funcs(task)
return
self_pool_appen d(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. 16 1476
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_pople ft = self.pool.pople ft
self_pool_appen d = self.pool.appen d
check = self.pool.__len __
try:
while check() > 0:
task = self_pool_pople ft()
task.next()
self_pool_appen d(task)
except StopIteration:
self.call_exit_ funcs(task)
return si**********@gm ail.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.
Yes. It slows down the loop when there are only a few iterators in the
pool, and speeds it up when there are > 2000.
My use case involves < 1000 iterators, so psyco is not much help. It
doesn't solve the magic creation of locals from instance vars either.
Sw. si**********@gm ail.com writes: My use case involves < 1000 iterators, so psyco is not much help. It doesn't solve the magic creation of locals from instance vars either.
How about using __slots__ to put those instance vars at fixed offsets
in the pool object (self then needs to be a new-style class instance).
That might or might not avoid the dict lookups.
> def loop(self): self_pool = self.pool self_call_exit_ funcs = self.call_exit_ funcs self_pool_pople ft = self.pool.pople ft self_pool_appen d = self.pool.appen d check = self.pool.__len __ while check() > 0: task = self_pool_pople ft() try: task.next() except StopIteration: self_call_exit_ funcs(task) return self_pool_appen d(task)
Stupid me. the 'return' statement above should be 'continue'. Sorry for
the confusion. si**********@gm ail.com napisał(a): def loop(self): self_pool = self.pool self_call_exit_ funcs = self.call_exit_ funcs self_pool_pople ft = self.pool.pople ft self_pool_appen d = self.pool.appen d check = self.pool.__len __ while check() > 0: task = self_pool_pople ft() try: task.next() except StopIteration: self_call_exit_ funcs(task) return self_pool_appen d(task)
Stupid me. the 'return' statement above should be 'continue'. Sorry for the confusion.
Then you can avoid continue by writing:
while check() > 0:
task = self_pool_pople ft()
try:
task.next()
except StopIteration:
self_call_exit_ funcs(task)
else:
self_pool_appen d(task)
Tomasz Lisowski
I still think there are savings to be had by looping inside the
try-except block, which avoids many setup/teardown exception handling
steps. This is not so pretty in another way (repeated while on
check()), but I would be interested in your timings w.r.t. your current
code.
def loop(self):
self_pool_pople ft = self.pool.pople ft
self_pool_appen d = self.pool.appen d
self_call_exit_ funcs = self.call_exit_ funcs
check = self.pool.__len __
while check() > 0:
try:
while check() > 0:
task = self_pool_pople ft()
task.next()
self_pool_appen d(task)
except StopIteration:
self_call_exit_ funcs(task)
-- Paul This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Simon Elliott |
last post by:
What optimisation do compilers typically provide when passing STL
containers around?
For example, if I do something like this:
struct Tbloggs
{
std::string s1;
};
typedef std::vector<Tbloggs> TbloggsList;
|
by: EC-AKD |
last post by:
Hi All,
I am new to the concept of optimising codes in C. I was wondering if
C level inlining of code is any way comparable to macros. I believe
that inlining is equivalent to writing macros. However I had this
strange feeling if I should go for macros wherever necessary instead
of inlining the codes.
In my project, I have to use basic...
|
by: Jon Maz |
last post by:
Hi,
I'm facing a code-optimisation issue on an asp.net/vb.net/SQL Server 2000
project. A web page containing not much more than 3 DropDownLists is taking
nigh on 6 seconds to load, because each ddl opens up a separate connection
to the DB, pulls out its data, and closes its own connection before the next
ddl repeats the process.
The...
|
by: David Welch |
last post by:
Hi,
I have a bit of code where I am relying on empty base member
optimisation. The bit of code is below:
template<typename Enum>
struct EncodePrefix
{
template<Enum e>
struct Apply
|
by: grid |
last post by:
Hi,
I was exploring the affect of cache on program
performance/optimisation.Is it the compilers responsibility only to
consider this kind of optimisation or the programmer can do his bit in
this case ?
Reading through the "Expert C Programming" text,it mentions how the
below program can be efficient taking the cache details into accont.
...
| |
by: per9000 |
last post by:
Hi,
I recently started working a lot more in python than I have done in
the past. And I discovered something that totally removed the pretty
pink clouds of beautifulness that had surrounded my previous python
experiences: magic names (I felt almost as sad as when I discovered
the strange pink worms that eat you in nethack, not to mention...
|
by: jyck91 |
last post by:
i have done the magic square:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 13
main()
{
FILE *fp;
int i, j, n, row, column;
|
by: special_dragonfly |
last post by:
Hello,
I know this might be a little cheeky, and if it is, please say, but I need a
little hand optimising some code. For the simple reason that this is
'company' code and I have no idea what I'm allowed to release and not as the
case may be I've changed anything that could give an indication of the
company - if that makes any sense...
for...
|
by: Larry Hale |
last post by:
I've heard tell of a Python binding for libmagic (file(1) *nixy
command; see http://darwinsys.com/file/). Generally, has anybody
built this and worked with it under Windows?
The only thing I've been able to find is the python-magic module at
http://hupp.org/adam/hg/python-magic/.
Is this "THE" python-magic module. (It seems to be to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |