By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,531 Members | 1,440 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,531 IT Pros & Developers. It's quick & easy.

Initializing Python in Optimized mode from C++

P: n/a
JT
Hi,

When embedding Python in C++, is there anyway to initialize the
interpreter so that it runs in optimized mode, equivalent to
specifying the -O flag when running the interpreter from the command
line?

Thanks,
John
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
JT wrote:
When embedding Python in C++, is there anyway to initialize the
interpreter so that it runs in optimized mode, equivalent to
specifying the -O flag when running the interpreter from the command
line?


here's one way to do it:

putenv("PYTHONOPTIMIZE=yes");
... initialize interpreter as usual ...

</F>


Jul 18 '05 #2

P: n/a
JT wrote:
Hi,

When embedding Python in C++, is there anyway to initialize the
interpreter so that it runs in optimized mode, equivalent to
specifying the -O flag when running the interpreter from the command
line?

Thanks,
John


set Py_OptimizeFlag to 1 for -O, and 2 for -OO. Do this prior to
calling Py_Initialize();

You can also improve your startup time by setting
Py_NoSiteFlag = 1...assuming you don't need to load site.py

for example:

extern int Py_OptimizeFlag;
extern int Py_NoSiteFlag;
....
if( !Py_IsInitialized() ) {
Py_OptimizeFlag = 2;
Py_NoSiteFlag = 1;
Py_Initialize();
}
Jul 18 '05 #3

P: n/a
JT
"Fredrik Lundh" <fr*****@pythonware.com> wrote in message
here's one way to do it:

putenv("PYTHONOPTIMIZE=yes");
... initialize interpreter as usual ...

</F>


Thanks for response! I'll try this solution. Does anyone know of any
way to do this directly through the Python C/C++ API, such as by
modifying a variable representing the state, interpreter or compiler,
or by calling a specific method?
Jul 18 '05 #4

P: n/a
"Rick Ratzel" <ri*********@magma-da.com> wrote in message
news:40***********************@news.twtelecom.net. ..
set Py_OptimizeFlag to 1 for -O, and 2 for -OO. Do this prior to
calling Py_Initialize();

You can also improve your startup time by setting
Py_NoSiteFlag = 1...assuming you don't need to load site.py

for example:

extern int Py_OptimizeFlag;
extern int Py_NoSiteFlag;
...
if( !Py_IsInitialized() ) {
Py_OptimizeFlag = 2;
Py_NoSiteFlag = 1;
Py_Initialize();
}


I was viewing this thread from Google groups and didn't see your response
before I replied. This is exactly what I was looking for...thanks! Out of
curiosity, what does the -OO flag do on top of the normal optimizations?
Jul 18 '05 #5

P: n/a
> I was viewing this thread from Google groups and didn't see your
response before I replied. This is exactly what I was looking
for...thanks! Out of curiosity, what does the -OO flag do on top of
the normal optimizations?


Strip """
Doc-Strings
"""

Harald
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.