473,804 Members | 2,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Optimizing constants in loops

The peephole optimizer now takes things like

if 0:
do_stuff()

and optimizes them away, and optimizes away the conditional in "if 1:".

What if I had a function like this?

def func(debug=Fals e):
for index in xrange(1000000) :
if debug:
print index
do_stuff(index)

Could the "if debug" be optimized away on function invocation if debug
is immutable and never reassigned in the function? When performance
really matters in some inner loop, I usually move the conditional
outside like this:

def func(debug=Fals e):
if debug:
for index in xrange(1000000) :
print index
do_stuff(index)
else:
for index in xrange(1000000) :
do_stuff(index)

It would be nice if this sort of thing could be done automatically,
either by the interpreter or a function decorator.
--
Michael Hoffman
Jun 13 '07 #1
2 1102
Michael Hoffman schrieb:
The peephole optimizer now takes things like

if 0:
do_stuff()

and optimizes them away, and optimizes away the conditional in "if 1:".

What if I had a function like this?

def func(debug=Fals e):
for index in xrange(1000000) :
if debug:
print index
do_stuff(index)

Could the "if debug" be optimized away on function invocation if debug
is immutable and never reassigned in the function? When performance
really matters in some inner loop, I usually move the conditional
outside like this:

def func(debug=Fals e):
if debug:
for index in xrange(1000000) :
print index
do_stuff(index)
else:
for index in xrange(1000000) :
do_stuff(index)

It would be nice if this sort of thing could be done automatically,
either by the interpreter or a function decorator.
Just use the builtin __debug__ variable for that purpose.
__debug__ is 'True' if Python is run normally, and 'False'
if run with the '-O' or '-OO' command line flag.
The optimizer works in the way you describe above (which
it will not if you use a custom variable).

Thomas

Jun 13 '07 #2
Thomas Heller wrote:
Just use the builtin __debug__ variable for that purpose.
__debug__ is 'True' if Python is run normally, and 'False'
if run with the '-O' or '-OO' command line flag.
The optimizer works in the way you describe above (which
it will not if you use a custom variable).
Thanks, I didn't know that __debug__ was optimized like this. But that
was really just a specific example of the general case.
--
Michael Hoffman
Jun 13 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2322
by: Andreas Paasch | last post by:
I've finally gotten my nice little system working and it's gone live. Now, I spent time optimizing my code and adding a little smart functionality here and there, based on needs and simplicity. In this context, I was wondering about something. I have a growing include.inc file that holds all my functions in it. Thinking speed, I was thinking that it might be a bit faster to split that include.inc file up into the different functions...
15
1789
by: Jacek Generowicz | last post by:
I have a multiple disptacher which, conceptually, looks something like this: class Multimethod: def __init__(self): self.methods = {}
16
2282
by: JustSomeGuy | last post by:
I have a routine that evaluates a polynomial equation that have 3 variables x,y,z of orders 1,2,3 the coefficients of the polynomial are in an array. This routine is quite slow and I'd like to optimize it. Any suggestions? simply put pts is a vector of typdef struct double x; double y; double z;
9
3936
by: Tzu-Chien Chiu | last post by:
Hi, "What methods do you ever use to optimize the programs?" We're developing a graphics chip emulator in C++, but it's very slow for big scenes. Even though this is a cross-platform software, I only want to optimize it on the Win32 (Windows 2000/XP) platform. I've tried the following method to optimize it:
35
8364
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except for '5'. So in his switch statement, he omits a case for '5':
8
1953
by: Hagen | last post by:
Hi, I have a question that you probably shouldn´t worry about since the compiler cares for it, but anyways: When you run your compiler with optimization turned on (eg. g++ with -Ox flag) and your program gets significantly faster than without, did you write bad code/ have a bad design? Cause what happens in those optimization steps is, I think, mostly
5
1669
by: Daishi Harada | last post by:
Hi, I'd like to get the 'get2' function below to perform like the 'get1' function (I've included timeit.py results). I'm not sure how to write 'mkget' to do achieve this, however, except to use 'exec' - is that what would be necessary?
6
2514
by: peter_k | last post by:
Hi, Last time i'm interested in optimizing small c programs. On my studies we are sending the programs using the web interface to online judge. The person who will wrote the faster program get the bonus score. This are usually simple problems, like sorting small numbers, parsing the text and checking something etc... To get the bonus the good algorithm is not everything, you have to do a lot of optimizations on c level (or asembler...
3
1994
by: Martin Pöpping | last post by:
Hello, is there a way to optimize loops (f.e. matrix operations) for multi processor systems with C#? I´m searching for something like OpenMP, which is available for C/C++ but not for C#. Or isn´t there any way to optimize it with C#?
13
1341
by: William McBrine | last post by:
Hi all, I'm pretty new to Python (a little over a month). I was wondering -- is something like this: s = re.compile('whatever') def t(whatnot): return s.search(whatnot)
0
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
jinu1996
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10351
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10096
tracyyun
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7638
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6866
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3834
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.