Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pandadefi/aebe11180230a8a5c642ee632d94c3ee to your computer and use it in GitHub Desktop.
Save pandadefi/aebe11180230a8a5c642ee632d94c3ee to your computer and use it in GitHub Desktop.
Gas savings - Variable initialization in loop
Display the source blob
Display the rendered blob
Raw
{
"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