Lazily import pandas to speedup non-pandas use #2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I'm using pytablewriter in some CLIs, and noticed they were sometimes slow so I profiled it.
Here's how to use
python -X importtime
and tuna to identify bottlenecks: https://medium.com/alan/how-we-improved-our-python-backend-start-up-time-2c33cd4873c8For example with this test script:
Then run:
Shows most of the import time comes from pandas:
It takes almost half a second to import pandas, 78.1% of the total 0.63s time.
This is not surprising, pandas and its dependency NumPy are big libraries.
However with this PR, if we lazily import pandas, that is, only import it when needed, we get a big speedup for all the non-pandas use cases, which covers a lot of pytablewriter's formats:
Now it only takes 0.164s for the whole program, a huge improvement over the 0.63s before, and very noticeable on the command line.
Another quick before and after comparison: