Created
September 24, 2021 10:49
-
-
Save rdettai/69b72d3cf0a89bc00eedd3df1a554c50 to your computer and use it in GitHub Desktop.
Test impact of opening and closing file descriptors
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": "code", | |
"execution_count": 1, | |
"id": "e262e2fb-a0ea-4291-92f1-b480b6f9743d", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import time\n", | |
"import os" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "8f68debb-56e9-40dd-a648-de4a795f5c7e", | |
"metadata": {}, | |
"source": [ | |
"# 1000 opening to write 10KB each time" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "d101bed6-2904-4685-88b1-72c7e46ea995", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.12156033515930176\n" | |
] | |
} | |
], | |
"source": [ | |
"start = time.time()\n", | |
"f=open('test1.txt', 'wb')\n", | |
"for i in range(1000):\n", | |
" f.write(os.urandom(10000))\n", | |
"f.close()\n", | |
"end = time.time()\n", | |
"print(end - start)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "86c36688-383f-4bf4-b6af-352d17248238", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.2606394290924072\n" | |
] | |
} | |
], | |
"source": [ | |
"start = time.time()\n", | |
"for i in range(1000):\n", | |
" f=open('test2.txt', 'wb')\n", | |
" f.write(os.urandom(10000))\n", | |
" f.close()\n", | |
"\n", | |
"end = time.time()\n", | |
"print(end - start)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "42ee3816-4267-4aac-9023-b1630e07887c", | |
"metadata": {}, | |
"source": [ | |
"# 100 opening to write 100KB each time" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "cfcc135c-9f47-4e4b-81c3-f34c9c00549f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.11486291885375977\n" | |
] | |
} | |
], | |
"source": [ | |
"start = time.time()\n", | |
"f=open('test1.txt', 'wb')\n", | |
"for i in range(100):\n", | |
" f.write(os.urandom(100000))\n", | |
"f.close()\n", | |
"end = time.time()\n", | |
"print(end - start)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "f91f84c8-6937-4032-bcd7-7e1c5a1c974d", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.14225053787231445\n" | |
] | |
} | |
], | |
"source": [ | |
"start = time.time()\n", | |
"for i in range(100):\n", | |
" f=open('test2.txt', 'wb')\n", | |
" f.write(os.urandom(100000))\n", | |
" f.close()\n", | |
"\n", | |
"end = time.time()\n", | |
"print(end - start)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"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.8.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment