473,323 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 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 2979
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

114
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
1
by: Paulo Eduardo Neves | last post by:
This is more of a shell or env question, but I believe others here have gone through this. I want to run an optimized python using the portable /usr/bin/env, but the obvious ways aren't working....
53
by: Michael Tobis | last post by:
Someone asked me to write a brief essay regarding the value-add proposition for Python in the Fortran community. Slightly modified to remove a few climatology-related specifics, here it is. I...
2
by: Gary Robinson | last post by:
I'm in the market for a server to run some python code which is optimized via psyco. Sun T2100 servers come with Solaris 10, which comes with python pre-installed. Since those servers use the...
5
by: Jon Smirl | last post by:
Is there some way to tell a dictionary object that I am going to load 1M objects into it and have it pre-allocate enought slots to hold all of the entries? Thus avoiding many thousand memory...
7
by: bonk | last post by:
I have a c# project as part of a larger VS 2005 solution that always gets build optimized and I therefore can not evaluate any values while debugging through the code ("Cannot evaluate expression...
14
by: Sugandh Jain | last post by:
Hi, The warning from Microsoft.Performance Code Analysis check that, its not required to initialize numeric variables to zero, boolean to false and object to null is a good one because CLR does...
23
by: Python Maniac | last post by:
I am new to Python however I would like some feedback from those who know more about Python than I do at this time. def scrambleLine(line): s = '' for c in line: s += chr(ord(c) | 0x80)...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.