comtypes
comtypes copied to clipboard
Iterating a collection using list() is very slow
Hello, I'm using comtypes to automate Microsoft Word and I've run into a strange performance issue. The following code takes 14 seconds to execute on my machine:
cells_in_table = [
list(row.Cells) for row in table.Rows
]
However this code takes only 4:
cells_in_table = []
for row in _rows:
cells_in_table.append([row.Cells(i+1) for i in range(len(row.Cells))])
As far as I can tell using list to enumerate a collection is a lot slower than manually iterating it. For example the following code takes 6.1 seconds:
list(_rows[0].Cells)
But this code takes 1.1 seconds:
[_rows[0].Cells(idx+1) for idx in range(len(_rows[0].Cells))]
What could be causing the discrepancy? Working with COM is ugly enough and I like being able to use pythonic constructs, having to abandon list() due to performance reasons is not great. Surely the __iter__ implementation for COM objects could just be self(x+1) for x in range(len(self)) for most containers?
I suspect this may be related: http://stackoverflow.com/questions/39225263/why-is-ctypes-so-slow-to-convert-a-python-list-to-a-c-array
Changing the implementation of __iter__ would have large impacts.
Besides, if the COM object has a _NewEnum, the Iterator implementation should be based on it.
As far as I can tell using
listto enumerate a collection is a lot slower than manually iterating it. For example the following code takes 6.1 seconds:
list(_rows[0].Cells)But this code takes 1.1 seconds:
[_rows[0].Cells(idx+1) for idx in range(len(_rows[0].Cells))]
This is more of tips for working with Excel in comtypes.
Since Excel has cells from A1 to XFD1048576, there are cases where the amount of calculation is enormous.
I will close this issue because no activity for over 5 years and it is something that should be resolved on the ctypes or COM library side.