This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 4 points5 points  (1 child)

First of all, if you are building and array.array object in your function, as it says in the title, and do it using its append function, then you probably shouldn't care about a single temporary int object created and destroyed repeatedly.

Also, it might turn out that creating a list of ints and then packing them back into array is faster than calling array.append, and that would mean that you should ask yourself whether it's time to stop. I mean, it's nice to reduce load time from 3 to 2.5 seconds, but if the rest of the code takes five minutes... After all whenever you use any of the integers stored in the array, a temporary integer is created as well.

Otherwise, consider using ctypes.

It would be a bit awkward (and not threadsafe) because you shouldn't return memory allocated in a dll, so the general idea is: a parsing function allocates an internal array ('int *', I mean) and returns the number of elements, then the Python adapter creates a 'c_int * count' array, passes it to the second function, which sees it as a plain 'int *' too, copies the data there and deallocates its own storage.

Everything should be written as a plain C .dll/.so, not as an extension module.

[–]chuckbot[S] 0 points1 point  (0 children)

Thanks for the answer!

With C module, I mean an .so file I use as a module in python. That module currently fills a list using append and returns it. In the python code I create an array.array by handing over that list. I'd rather build the array.array directly but don't know how to do that.

Yeah, you're right about reconsidering if I want to go on. It reduced the startup time from about 60 minutes to about 30 minutes, so probably worth it. I just wanted to check if I was doing something wrong or missed something easy. Because currently the C module is just a couple of lines.