Created
March 12, 2024 09:17
-
-
Save pandadefi/aebe11180230a8a5c642ee632d94c3ee to your computer and use it in GitHub Desktop.
Gas savings - Variable initialization in loop
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Gas savings: variable declaration in loop" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"The boa.ipython extension is already loaded. To reload it, use:\n", | |
" %reload_ext boa.ipython\n" | |
] | |
} | |
], | |
"source": [ | |
"%load_ext boa.ipython\n", | |
"import boa" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<boa.contracts.vyper.vyper_contract.VyperDeployer at 0x106d5b340>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"%%vyper TestLoops\n", | |
"\n", | |
"@external\n", | |
"def loop_with_init_inside():\n", | |
" for i in range(10):\n", | |
" a: uint256 = 0\n", | |
" a = i\n", | |
"\n", | |
"@external\n", | |
"def loop_with_init_outside():\n", | |
" a: uint256 = 0\n", | |
" for i in range(10):\n", | |
" a = i\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"652" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"loops = TestLoops()\n", | |
"loops.loop_with_init_inside()\n", | |
"loops._computation.get_gas_used()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"603" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"loops = TestLoops()\n", | |
"loops.loop_with_init_outside()\n", | |
"loops._computation.get_gas_used()" | |
] | |
} | |
], | |
"metadata": { | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment