⚡️ Speed up function load_json_from_string
by 93,682%
#34
+6
−0
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.
📄 93,682% (936.82x) speedup for
load_json_from_string
insrc/numpy_pandas/numerical_methods.py
⏱️ Runtime :
1.13 seconds
→1.21 milliseconds
(best of248
runs)📝 Explanation and details
Here’s the optimized version of your program, focused on eliminating redundant parsing. Currently, you're re-parsing the same JSON string 1000 times using the relatively slow
json.loads
(pure Python after import and not the fastest).A huge speedup can be gained by parsing once, then replicating using
[obj.copy() for _ in range(1000)]
, since the objects are all the same. For most JSON objects, a shallow copy is sufficient and much faster.If you really do need 1000 distinct copies (not references), use
.copy()
. If not, you can even just replicate references.If you need to use a faster parser, orjson is installed and is dramatically faster (it's a C extension). However,
orjson.loads
returns immutable types and may not always producedict
, so let's stick tojson
if you want mutability unless told otherwise.Here’s the rewritten, optimized function with explanations.
.copy()
is correct and fast for shallow dicts.orjson
if possible (if you can handle slightly different types, e.g.dict
vs.orjson
types).You may test which is fastest for your needs. All of these are massively faster than parsing the string 1000 times.
Summary:
.copy()
depending on your requirements.dict
return types, consider usingorjson
.💡 This modification will reduce your runtime by several orders of magnitude!
Comments:
If you want an absolute minimal/fast case and it's OK to return the same object repeatedly (not copies), just.
But this does NOT create distinct dicts—they’re all the same object in memory.
Let me know if you need the
orjson
version ordeepcopy
for nested objects!✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-load_json_from_string-mc9q73u8
and push.