471,073 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

Initializing Python in Optimized mode from C++

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
5 2885
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
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
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
"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
> 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.

Similar topics

114 posts views Thread by Maurice LING | last post: by
1 post views Thread by Paulo Eduardo Neves | last post: by
53 posts views Thread by Michael Tobis | last post: by
2 posts views Thread by Gary Robinson | last post: by
5 posts views Thread by Jon Smirl | last post: by
14 posts views Thread by Sugandh Jain | last post: by
reply views Thread by leo001 | last post: by

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.