473,379 Members | 1,191 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,379 software developers and data experts.

speeding things up with C++

I've done all the requisite profiling and thought fairly deeply about
the efficiency of my python code, but am still going to have to speed
up the innermost guts of what I am doing.

Essentially, I need to pass a list of 6-tuples containing only
integers to my new sadly necessary super-fast compiled language
function which i am not looking forward to writing:

input: [(1,2,3,4,5,6), (7,8,9,10,11,12),...]

and after much thrashing of processor resources, return data which
looks like this to the Python calling environment:

output: [( (1, 2), (1,), (12,), (13), (1, 7, 11), (9,) ), ( another
nested tuple like preceding one ), .... ]

Each member of the returned list is a tuple containing 6 tuples, and
each of those 6 tuples has at least one integer member. It would also
be acceptable for the return values to be entirely nested lists
instead of having the two innermost sequence types as tuples.

I probably want to be using something like C++ because i'm going to
need to use STL vectors and sets for what i'm doing in the algorithm
i'm trying to speed up. IIRC Boost tuple is a bit painful for more
than 10 elements + isn't really essential that I use a a non-mutable
data type in what will be a small encapsulated bit of a much larger
system.

Anyway, I can probably very quickly figure out some hacked way to get
the data into my function given that in the worst case I could take
advantage of the knowledge that each input tuple always has 6
elements, and simply pass in a big array of ints. Yes, I know this is
a bit retarded, but I'm talking worst case assuming on very tight
schedule and no time to delve deeply into SWIG or whatever. Similarly
it wouldn't be too difficult to return the result as the mother all of
all strings which i could then parse fairly easily.

However, I hope someone reading this will be able to tell me that I'm
being a total pessimist and that in fact it isn't very difficult to do
what I want to do using SWIG. I'm not asking for a complete solution,
more like some general pointers from someone who has actually done
something similar before.

As an added bonus, I wouldn't if this is 'easily' doable using Ocaml
as the guts instead of C++, I'd be happy to know about it.

May 26 '07 #1
11 1147
On May 26, 11:19 am, bullockbefriending bard <kinch1...@gmail.com>
wrote:
However, I hope someone reading this will be able to tell me that I'm
being a total pessimist and that in fact it isn't very difficult to do
what I want to do using SWIG. I'm not asking for a complete solution,
more like some general pointers from someone who has actually done
something similar before.
I do stuff like that with ctypes ( http://docs.python.org/lib/module-ctypes.html
)!
I'm sure you can figure out some way to pass/return data to/from your C
++ lib.
The easiest way - use strings or arrays, but if you dig deeper into
ctyles documentation,
I'm sure you will find a better solution!

Good luck,
-- misreckoning

May 26 '07 #2
thanks! i'll look into this.

On May 27, 5:35 am, Che Guevara <sreckotoro...@gmail.comwrote:
On May 26, 11:19 am, bullockbefriending bard <kinch1...@gmail.com>
wrote:
However, I hope someone reading this will be able to tell me that I'm
being a total pessimist and that in fact it isn't very difficult to do
what I want to do using SWIG. I'm not asking for a complete solution,
more like some general pointers from someone who has actually done
something similar before.

I do stuff like that with ctypes (http://docs.python.org/lib/module-ctypes.html
)!
I'm sure you can figure out some way to pass/return data to/from your C
++ lib.
The easiest way - use strings or arrays, but if you dig deeper into
ctyles documentation,
I'm sure you will find a better solution!

Good luck,
-- misreckoning

May 28 '07 #3
I wonder if Jython might be the answer? Java is going to be faster
than Python for the time-critical part of my program. Does anybody
have experience getting data structures like nested lists / tuples
into a java routine from a running jython program (and then back
again)?

May 28 '07 #4
On May 26, 11:19 am, bullockbefriending bard <kinch1...@gmail.com>
wrote:
I've done all the requisite profiling and thought fairly deeply about
the efficiency of my python code, but am still going to have to speed
up the innermost guts of what I am doing.

Essentially, I need to pass a list of 6-tuples containing only
integers to my new sadly necessary super-fast compiled language
function which i am not looking forward to writing:

input: [(1,2,3,4,5,6), (7,8,9,10,11,12),...]

and after much thrashing of processor resources, return data which
looks like this to the Python calling environment:

output: [( (1, 2), (1,), (12,), (13), (1, 7, 11), (9,) ), ( another
nested tuple like preceding one ), .... ]

Each member of the returned list is a tuple containing 6 tuples, and
each of those 6 tuples has at least one integer member. It would also
be acceptable for the return values to be entirely nested lists
instead of having the two innermost sequence types as tuples.

I probably want to be using something like C++ because i'm going to
need to use STL vectors and sets for what i'm doing in the algorithm
i'm trying to speed up. IIRC Boost tuple is a bit painful for more
than 10 elements + isn't really essential that I use a a non-mutable
data type in what will be a small encapsulated bit of a much larger
system.

Anyway, I can probably very quickly figure out some hacked way to get
the data into my function given that in the worst case I could take
advantage of the knowledge that each input tuple always has 6
elements, and simply pass in a big array of ints. Yes, I know this is
a bit retarded, but I'm talking worst case assuming on very tight
schedule and no time to delve deeply into SWIG or whatever. Similarly
it wouldn't be too difficult to return the result as the mother all of
all strings which i could then parse fairly easily.

However, I hope someone reading this will be able to tell me that I'm
being a total pessimist and that in fact it isn't very difficult to do
what I want to do using SWIG. I'm not asking for a complete solution,
more like some general pointers from someone who has actually done
something similar before.

As an added bonus, I wouldn't if this is 'easily' doable using Ocaml
as the guts instead of C++, I'd be happy to know about it.
A just too obvious recommendation is to drop C++ and use D together
with the Pyd/celerid bridge. Yeah, it sounds a little esoteric but D
is really the Python among the statically typed imperative languages.

http://www.digitalmars.com/d/
http://pyd.dsource.org/index.html
May 28 '07 #5
thanks. i'll definitely look into this.

On May 28, 10:48 pm, Kay Schluehr <kay.schlu...@gmx.netwrote:
On May 26, 11:19 am, bullockbefriending bard <kinch1...@gmail.com>
wrote:
I've done all the requisite profiling and thought fairly deeply about
the efficiency of my python code, but am still going to have to speed
up the innermost guts of what I am doing.
Essentially, I need to pass a list of 6-tuples containing only
integers to my new sadly necessary super-fast compiled language
function which i am not looking forward to writing:
input: [(1,2,3,4,5,6), (7,8,9,10,11,12),...]
and after much thrashing of processor resources, return data which
looks like this to the Python calling environment:
output: [( (1, 2), (1,), (12,), (13), (1, 7, 11), (9,) ), ( another
nested tuple like preceding one ), .... ]
Each member of the returned list is a tuple containing 6 tuples, and
each of those 6 tuples has at least one integer member. It would also
be acceptable for the return values to be entirely nested lists
instead of having the two innermost sequence types as tuples.
I probably want to be using something like C++ because i'm going to
need to use STL vectors and sets for what i'm doing in the algorithm
i'm trying to speed up. IIRC Boost tuple is a bit painful for more
than 10 elements + isn't really essential that I use a a non-mutable
data type in what will be a small encapsulated bit of a much larger
system.
Anyway, I can probably very quickly figure out some hacked way to get
the data into my function given that in the worst case I could take
advantage of the knowledge that each input tuple always has 6
elements, and simply pass in a big array of ints. Yes, I know this is
a bit retarded, but I'm talking worst case assuming on very tight
schedule and no time to delve deeply into SWIG or whatever. Similarly
it wouldn't be too difficult to return the result as the mother all of
all strings which i could then parse fairly easily.
However, I hope someone reading this will be able to tell me that I'm
being a total pessimist and that in fact it isn't very difficult to do
what I want to do using SWIG. I'm not asking for a complete solution,
more like some general pointers from someone who has actually done
something similar before.
As an added bonus, I wouldn't if this is 'easily' doable using Ocaml
as the guts instead of C++, I'd be happy to know about it.

A just too obvious recommendation is to drop C++ and use D together
with the Pyd/celerid bridge. Yeah, it sounds a little esoteric but D
is really the Python among the statically typed imperative languages.

http://www.digitalmars.com/d/http://...org/index.html

May 28 '07 #6
On 26 May 2007 02:19:39 -0700, bullockbefriending bard <ki*******@gmail.comwrote:
....
Essentially, I need to pass a list of 6-tuples containing only
integers to my new sadly necessary super-fast compiled language
function which i am not looking forward to writing:

input: [(1,2,3,4,5,6), (7,8,9,10,11,12),...]

and after much thrashing of processor resources, return data which
looks like this to the Python calling environment:

output: [( (1, 2), (1,), (12,), (13), (1, 7, 11), (9,) ), ( another
nested tuple like preceding one ), .... ]
....
However, I hope someone reading this will be able to tell me that I'm
being a total pessimist and that in fact it isn't very difficult to do
what I want to do using SWIG.
You're talking about the actual conversion between Python data
structures and C or C++ data structures? That is easy to do even
manually, IMHO -- provided a decent C background.

Have a look in the Python/C API Reference Manual, and the mapping
becomes clear. The PyListObject stuff for example, where there's a C
function for every basic operation on lists, and where the elements
have the C type PyObject. And so on. Mapping to C is just a matter of
walking a nested data structure, where you have a good idea what it is
supposed to look like (a list of six-tuples of some kind of numbers).

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org R'lyeh wgah'nagl fhtagn!
May 30 '07 #7
Thanks this is good news. I think my C/C++ background is sufficient to
manage to figure things out if I RTFM carefully.

Basically I want to pass in a Python list of integer tuples, create an
STL container full of equivalent tuples, apply some processor-
intensive algorithm to said list of tuples, and finally poke the
results back into another Python list of integer tuples and return it
to the calling Python environment. Data structures are well-defind and
simple, and the most complex case would be 3-deep nested list, so I
will seriously consider figuring out how to do it manually as you
suggest.

On May 31, 3:04 am, Jorgen Grahn <grahn+n...@snipabacken.dyndns.org>
wrote:
On 26 May 2007 02:19:39 -0700, bullockbefriending bard <kinch1...@gmail.comwrote:
...
Essentially, I need to pass a list of 6-tuples containing only
integers to my new sadly necessary super-fast compiled language
function which i am not looking forward to writing:
input: [(1,2,3,4,5,6), (7,8,9,10,11,12),...]
and after much thrashing of processor resources, return data which
looks like this to the Python calling environment:
output: [( (1, 2), (1,), (12,), (13), (1, 7, 11), (9,) ), ( another
nested tuple like preceding one ), .... ]
...
However, I hope someone reading this will be able to tell me that I'm
being a total pessimist and that in fact it isn't very difficult to do
what I want to do using SWIG.

You're talking about the actual conversion between Python data
structures and C or C++ data structures? That is easy to do even
manually, IMHO -- provided a decent C background.

Have a look in the Python/C API Reference Manual, and the mapping
becomes clear. The PyListObject stuff for example, where there's a C
function for every basic operation on lists, and where the elements
have the C type PyObject. And so on. Mapping to C is just a matter of
walking a nested data structure, where you have a good idea what it is
supposed to look like (a list of six-tuples of some kind of numbers).

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org R'lyeh wgah'nagl fhtagn!

May 31 '07 #8
On 31 May 2007 03:45:32 -0700, bullockbefriending bard
<ki*******@gmail.comwrote:
Thanks this is good news. I think my C/C++ background is sufficient to
manage to figure things out if I RTFM carefully.

Basically I want to pass in a Python list of integer tuples, create an
STL container full of equivalent tuples, apply some processor-
intensive algorithm to said list of tuples, and finally poke the
results back into another Python list of integer tuples and return it
to the calling Python environment. Data structures are well-defind and
simple, and the most complex case would be 3-deep nested list, so I
will seriously consider figuring out how to do it manually as you
suggest.
Are you sure you want an STL container? Since the primary operator
here is Python, the extra benefits from the STL container over plain C
arrays isn't as evident.

Pyrex is a good way to write the interface between your C++ code and
the Python code - it handles the refcounting and boilerplate for you -
and perhaps for writing the algorithms as well, depending on how
complicated and performance sensitive they are.

Also, using numeric/Numarray can be a very big win. It can potentially
save you a fair amount of marshalling overhead.
May 31 '07 #9
Are you sure you want an STL container? Since the primary operator
here is Python, the extra benefits from the STL container over plain C
arrays isn't as evident.

Pyrex is a good way to write the interface between your C++ code and
the Python code - it handles the refcounting and boilerplate for you -
and perhaps for writing the algorithms as well, depending on how
complicated and performance sensitive they are.
good point. while i bow to the genius of the folks who invented
template metaprogramming, the compiler error messages tend to be
profoundly depressing :). one way or the other, pyrex is something i
need to learn since i'm now completely enamoured with python and had
better develop an arsenal of tricks for the rare times when it's just
not fast enough.
Also, using numeric/Numarray can be a very big win. It can potentially
save you a fair amount of marshalling overhead.
as i understand it, this is so for applying the likes of matrix
operations, autocorrelations, FFTs, etc...where python essentially
provides scripting glue to some highly optimised C functions. i'm
assuming that the kind of algorithm i am looking at which involves
some set operations on list elements + copying between lists isn't
going to be helped so much by using numpy or similar.
Jun 1 '07 #10
bullockbefriending bard skrev:
good point. while i bow to the genius of the folks who invented
template metaprogramming, the compiler error messages tend to be
profoundly depressing :). one way or the other, pyrex is something i
need to learn since i'm now completely enamoured with python and had
better develop an arsenal of tricks for the rare times when it's just
not fast enough.
A dash of c combined integrated via ctypes is probably the easiest solution?
--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
Jun 1 '07 #11
On Thu, 31 May 2007 12:25:17 -0500, Chris Mellon <ar*****@gmail.comwrote:
On 31 May 2007 03:45:32 -0700, bullockbefriending bard
<ki*******@gmail.comwrote:
>Thanks this is good news. I think my C/C++ background is sufficient to
manage to figure things out if I RTFM carefully.

Basically I want to pass in a Python list of integer tuples, create an
STL container full of equivalent tuples, apply some processor-
intensive algorithm to said list of tuples, and finally poke the
results back into another Python list of integer tuples and return it
to the calling Python environment. Data structures are well-defind and
simple, and the most complex case would be 3-deep nested list, so I
will seriously consider figuring out how to do it manually as you
suggest.

Are you sure you want an STL container? Since the primary operator
here is Python, the extra benefits from the STL container over plain C
arrays isn't as evident.
STL containers are easier to use, harder to misuse and take care of
memory allocations. Wouldn't you say that's a benefit? If I wrote an
algorithm in C++, I'd rather pass it a const std::vector<double>& than
a const double * and a length.

That said, I prefer C for simple extension modules which just wrap
something. Less dependencies for the person who builds it, more people
who understand it well enough to change it.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org R'lyeh wgah'nagl fhtagn!
Jun 2 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Spamtrap | last post by:
I only do occasional Perl programming and most things I write are short processes. I have something I'm working on that is scanning a text file with about 15 million lines and trying to extract...
4
by: Jm | last post by:
Hi all I know this might sound a little stupid but i thought id better ask anyway in case there are some things i havent already tried that everybody else knows. Basically i am wondering if...
11
by: Dan Sugalski | last post by:
Is there any good way to speed up SQL that uses like and has placeholders? Here's the scoop. I've got a system that uses a lot of pre-generated SQL with placeholders in it. At runtime these SQL...
2
by: salad | last post by:
This is a tip on how to speed up listboxes DRAMATICALLY. Persons that would benefit are those that are constantly updating the rowsource of a listbox/combobox in order to filter and sort the data...
5
by: RobinAG | last post by:
Hello, I just split my database into front and back end. My front end users are experiencing really slow opening of forms. I've searched online for help speeding up forms, but I'm not sure what...
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.