Avoiding boost::python::extract<int> - python

I am running a rather simple task that is being handicapped by the use of boost::python::extract. In short I have a very large python list containing only integers. I need to pass those integer values to a C++ map using the find function. In order to do the hash using the contents of the array I need to convert those contents (a python list object) into an int. I can guarantee from my workflow that only ints will be passed to this list
Because my array is so large I have looked into multithreading but it seems that whenever I have to threads try to read from the array and use the boost::python::extract function I get a SegFault.
I am wondering if there is an alternative to boost::python::extract or a better representation than boost::python::list. One in which C++ can explicitly tell that the contents are ints without me having to step through and convert each element one at a time (which currently takes several seconds).
Thank you

Related

C++ - How to take a large array as input for a function

I have a function that takes in a couple of large multi-dimension float arrays as input. As a result, I crash the stack.
I'm a beginner to C/C++ so I will apologize in advance if this question is dumb. Anyways, after looking around the net, this problem is not particularly new and the general solution is either make it as a global variable or use vector (instead of array).
However, this piece of C++ code was intended to be used as a shared library and takes input from a Python script and returns (or modifies) the value (also in arrays). Therefore I don't think it would be possible to declare as global (correct me if I am wrong).
So, apart from using vector, is there any other way to do so? The reason that put me off from using C++ vector is there is no equivalent data type in Python. I'm using ctypes for communication between Python and C++ if that matters.
There is no way in C/C++ to pass an array as a function argument by value. You can pass a pointer to the array and the size of the array. In C++, you also can pass a reference to the array and the size of the array (in template parameters). In either case, the array is not copied and the called function accesses the array in-place, where the caller allocated it.
Note that the array can be wrapped in a structure, in which case it can be passed by value. In C++, std::array is an example of such. But the caller has to initialize that structure instead of the raw array. std::vector is similar in that it is a structure and it automatically copies elements when the vector is copied, but unlike C arrays and std::array it dynamically allocates memory for the elements.
However, if you're integrating with C API, you are most likely limited to a pointer+size or pointer+size wrapped in a C structure solutions. The rules of working with the array (e.g. who allocates and frees the array, are writes to the array allowed, etc.) are specific to the particular API you're working with. Often, there is a dedicated set of functions for working with arrays provided by the library. You should read the API documentation about conventions taken in Python.

Data type to save expanding data for data logging in Python

I am writing a serial data logger in Python and am wondering which data type would be best suited for this. Every few milliseconds a new value is read from the serial interface and is saved into my variable along with the current time. I don't know how long the logger is going to run, so I can't preallocate for a known size.
Intuitively I would use an numpy array for this, but appending / concatenating elements creates a new array each time from what I've read.
So what would be the appropriate data type to use for this?
Also, what would be the proper vocabulary to describe this problem?
Python doesn't have arrays as you think of them in most languages. It has "lists", which use the standard array syntax myList[0] but unlike arrays, lists can change size as needed. using myList.append(newItem) you can add more data to the list without any trouble on your part.
Since you asked for proper vocabulary in a useful concept to you would be "linked lists" which is a way of implementing array like things with varying lengths in other languages.

Is it computationally faster to change a list to a tuple before moving it to another function?

I create a list of 24 floats that is needed in a calling function. The calling function will not need to alter the floats, so it can work with a tuple. Is it computationally faster to change the list to a tuple, tuple(list), before returning it to the calling function rather than passing and then using the list the entire time?
A corollary to this is: Should I change a list to a tuple within a function if the function can work with the tuple? I have many instances of creating a list, then using it later in the same function where a tuple of the list would work.
I have several instances of this in my program, so any speed advantage would be helpful to overall performance.
I don't know how to time these things and cannot find a past similar question. I know tuples are more about 3 times faster.
Code sample not needed.
After the creation it will not matter anymore so if you want to improve your code than think before the creation if you need a tuple or a list.be aware that tuple are fixed size and list are dynamic so it ll depend on what you are trying to do.
after creation it doesnt actually matter because accessing elements is not faster or anything and it doesnt make sense to convert a list to tuple after creation that will not make your code faster or more efficient. you can look here for more link
However to test the execution Time you can use the timeit module
import timeit
start = timeit.default_timer()
# your function or piece of code
end= timeit.default_timer()
print(end-start)

Setting values in large tuples efficiently

I am using ROS and I am currently trying to munipulate a costmap, so essentially I am wanting to change individual values in a tuple that has a length in this situation of approaching 7 digits.
See http://docs.ros.org/api/nav_msgs/html/msg/OccupancyGrid.html
Originally I tried just turning this long tuple into a list then changing the values and then turning this back into a tuple, as you can imagine this extremely inefficient. I need this to be able to run quickly as it needs to update the costmap often for dynamic object avoidance.
Is there a way that I can change individual values in a tuple efficiently?
Sadly, this is simply a limitation of ROS's python message data model. Array-like structures are always deserialized as tuple for performance reasons, except for lists of bool for some reason. And tuple is immutable.
However, if you were in C++ space, you would be receiving a const OccupancyGridConstPtr& anyway, so it would still be just as immutable. Or you could have registered the callback as OccupancyGrid message and get pass-by-value, but you're just moving the copy to method-call-time. There's no avoiding the copy if you intend on modifying the grid, wether you're in Python or C++.
There is no need to convert back to tuple however, ROS's python message serialization accepts either list or tuple.
You can gain quite a bit of efficiency as well if you can do some of your processing work during that copy (saves an iteration over the grid). Though I don't know exactly what you're trying to do, so I don't know the feasibility of that.
The fact that tuples are immutable is supposed to be a feature, not a bug.
So if you need to make changes, the data probably ought not to be in tuple form in the first place.
Can't you have a list from begin to end? (I do not know ROS)
If that is impossible, I'd say you either have a serious problem with the software design or you are trying to solve the wrong problem.

Since Python lists can hold elements of different types, is accessing an element worse than constant time?

Languages such as C++ require that an array hold elements of a single type. As I understand it, knowing the size of each element allows for pointer arithmetic, making access of a particular element O(1) time.
What about Python lists?
Python lists allow for mixing element types. Surely the implementation doesn't involve a slow-access data structure, such as a linked lists – right? Is accessing an element even constant time? If so, how does Python achieve it with variable element types?
Its a simple indexed lookup. Python stores references to objects in its lists, not the objects themselves. Consider a C++ list of (void*) pointers. Each pointer is a known size and array lookup is fast, but the things it points to can vary in size.
In Python, everything is an "object" (you can intuitively confirm that by something like (1).__add__(2)). So, roughly speaking, Python's list just contain references to the actual objects stored somewhere in memory. And if you look up an object via the list index - this is very, very simplified - it will redirect you to the actual object.
Here is a nice table that shows you the complexity (Big-Oh) of the different operations on lists.

Categories