-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize copy behavior of expressions
- Loading branch information
Showing
12 changed files
with
275 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def numerify_special_constants(prefix_expression: list[str], inplace: bool = False, append: bool = False) -> list[str]:\n", | ||
" # print(id(prefix_expression))\n", | ||
"\n", | ||
" if not inplace:\n", | ||
" if not append:\n", | ||
" modified_prefix_expression = prefix_expression.copy()\n", | ||
" for i, token in enumerate(modified_prefix_expression):\n", | ||
" if token == '2':\n", | ||
" modified_prefix_expression[i] = '?'\n", | ||
" else:\n", | ||
" modified_prefix_expression = []\n", | ||
" for i, token in enumerate(prefix_expression):\n", | ||
" if token == '2':\n", | ||
" modified_prefix_expression.append('?')\n", | ||
" modified_prefix_expression.append(token)\n", | ||
" else:\n", | ||
" modified_prefix_expression = prefix_expression\n", | ||
"\n", | ||
" # print(id(modified_prefix_expression))\n", | ||
"\n", | ||
" modified_prefix_expression[0] = '?'\n", | ||
" \n", | ||
" # print(id(modified_prefix_expression))\n", | ||
"\n", | ||
" return modified_prefix_expression" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"139663546625344\n", | ||
"139663546688832\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"my_list = ['1', '2', '3']\n", | ||
"print(id(my_list))\n", | ||
"my_returned_list = numerify_special_constants(my_list, inplace=False)\n", | ||
"print(id(my_returned_list))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"139663546619072\n", | ||
"139663546584640\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"my_list = ['1', '2', '3']\n", | ||
"print(id(my_list))\n", | ||
"my_returned_list = numerify_special_constants(my_list, inplace=False, append=True)\n", | ||
"print(id(my_returned_list))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"139663546562240\n", | ||
"139663546562240\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"my_list = ['1', '2', '3']\n", | ||
"print(id(my_list))\n", | ||
"my_returned_list = numerify_special_constants(my_list, inplace=True)\n", | ||
"print(id(my_returned_list))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"142 ns ± 3.21 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"%%timeit\n", | ||
"numerify_special_constants(['1', '2', '3'], inplace=False)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"158 ns ± 2.51 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"%%timeit\n", | ||
"numerify_special_constants(['1', '2', '3'], inplace=False, append=True)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"47.7 ns ± 0.747 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"%%timeit\n", | ||
"numerify_special_constants(['1', '2', '3'], inplace=True)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "flash-ansr", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.10" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -404,3 +404,7 @@ def main(argv: str = None) -> None: | |
case _: | ||
parser.print_help() | ||
sys.exit(1) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.