Initializing Python in Optimized mode from C++

Rick Ratzel rick.ratzel at magma-da.com
Fri Apr 16 18:51:07 EDT 2004


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();
}



More information about the Python-list mailing list