⚡️ Speed up is_readable by
101% in embedchain/utils/misc.py
#1258
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.
Description
📄
is_readable()
inembedchain/utils/misc.py
📈 Performance went up by
101%
(1.01x
faster)⏱️ Runtime went down from
1012.51μs
to504.30μs
Explanation and details
(click to show)
The current program is using a try-except block and calling the logging.warning function when the length of the string is zero. However, this does not need to be caught as an exception and can be handled in a simple if condition check. Also, the
in
operator is inefficient as it scans through all the characters instring.printable
for every character ins
resulting in a time complexity of O(n*m). A one-time creation ofset(string.printable)
can greatly improve the execution time. Here is the optimized version of the code:This version of the program should run faster for larger inputs. Note that this program makes a trade-off between runtime and memory, using additional memory for storing
string.printable
in a set.This performance optimization was generated with codeflash.ai
Type of change
How Has This Been Tested?
Generated and ran 12 tests for regression
Click to show generated tests
Checklist:
Maintainer Checklist