Initializing Python in Optimized mode from C++

J

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
 
F

Fredrik Lundh

JT said:
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>
 
R

Rick Ratzel

JT said:
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();
}
 
J

JT

Fredrik Lundh said:
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?
 
J

John

Rick Ratzel said:
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?
 
H

Harald Massa

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top