Created
March 9, 2023 04:33
-
-
Save chienhsiang-hung/3b335babe08551db2a69e80ad1949893 to your computer and use it in GitHub Desktop.
iterate through list and tuple / How To Efficiently Concatenate Strings In Python
This file contains hidden or 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, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"100%|██████████| 100/100 [00:03<00:00, 30.00it/s]\n", | |
" 1%|▏ | 13/1000 [00:00<00:07, 128.83it/s]" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"assigning test Starts...\n", | |
"avg time_for_assing: 0.010596342086791992\n", | |
"avg time_for_not_ass: 0.010336775779724122\n", | |
"avg time_for_tuple: 0.01205378293991089\n", | |
"assigning test Ends...\n", | |
"\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"100%|██████████| 1000/1000 [00:06<00:00, 152.01it/s]\n", | |
" 0%| | 0/100 [00:00<?, ?it/s]" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"purely test the difference btw list and tuple Starts...\n", | |
"avg time_for_tuple: 0.0033366718292236327\n", | |
"avg time_for_list: 0.0031881237030029295\n", | |
"purely test the difference btw list and tuple Ends...\n", | |
"\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"100%|██████████| 100/100 [01:38<00:00, 1.02it/s]" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"test different way to concat the str in python Starts...\n", | |
"avg time_for_t: 0.71734628200531\n", | |
"avg time_for_l: 0.261464569568634\n", | |
"test different way to concat the str in python Ends...\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"import time\n", | |
"from tqdm import tqdm\n", | |
"\n", | |
"\n", | |
"time_for_assing = []\n", | |
"time_for_not_ass = []\n", | |
"time_for_tuple = []\n", | |
"n = 250000\n", | |
"\n", | |
"for _ in tqdm(range(100)):\n", | |
" # assign\n", | |
" start = time.time()\n", | |
" a = ('a '*n).split()\n", | |
" for _ in a:\n", | |
" pass\n", | |
" time_for_assing.append(time.time()-start)\n", | |
"\n", | |
" # no assign\n", | |
" start = time.time()\n", | |
" for _ in ('a '*n).split():\n", | |
" pass\n", | |
" time_for_not_ass.append(time.time()-start)\n", | |
"\n", | |
" # tuple\n", | |
" start = time.time()\n", | |
" for _ in tuple(('a '*n).split()):\n", | |
" pass\n", | |
" time_for_tuple.append(time.time()-start)\n", | |
" \n", | |
"print('assigning test Starts...')\n", | |
"print(f'avg time_for_assing: {sum(time_for_assing)/len(time_for_assing)}')\n", | |
"print(f'avg time_for_not_ass: {sum(time_for_not_ass)/len(time_for_not_ass)}')\n", | |
"print(f'avg time_for_tuple: {sum(time_for_tuple)/len(time_for_tuple)}')\n", | |
"print('assigning test Ends...\\n')\n", | |
"\n", | |
"\n", | |
"# purely test the difference btw list and tuple\n", | |
"time_for_t = []\n", | |
"time_for_l = []\n", | |
"n = 100000\n", | |
"\n", | |
"for _ in tqdm(range(1000)):\n", | |
" start = time.time()\n", | |
" for i in tuple(range(n)):\n", | |
" pass\n", | |
" time_for_t.append(time.time()-start)\n", | |
"\n", | |
" start = time.time()\n", | |
" for i in list(range(n)):\n", | |
" pass\n", | |
" time_for_l.append(time.time()-start)\n", | |
"\n", | |
"print('purely test the difference btw list and tuple Starts...')\n", | |
"print(f'avg time_for_tuple: {sum(time_for_t)/len(time_for_t)}')\n", | |
"print(f'avg time_for_list: {sum(time_for_l)/len(time_for_l)}')\n", | |
"print('purely test the difference btw list and tuple Ends...\\n')\n", | |
"\n", | |
"\n", | |
"# test different way to concat the str in python\n", | |
"time_for_t = []\n", | |
"time_for_l = []\n", | |
"n = 3000000\n", | |
"s = 'a'*n\n", | |
"\n", | |
"for _ in tqdm(range(100)):\n", | |
" start = time.time()\n", | |
" tmp = ''\n", | |
" for ss in s:\n", | |
" tmp += ss\n", | |
" time_for_t.append(time.time()-start)\n", | |
"\n", | |
" start = time.time()\n", | |
" s_list = []\n", | |
" for ss in s:\n", | |
" s_list.append(ss)\n", | |
" s_list = ''.join(s_list)\n", | |
" time_for_l.append(time.time()-start)\n", | |
"\n", | |
"print('test different way to concat the str in python Starts...')\n", | |
"print(f'avg time_for_t: {sum(time_for_t)/len(time_for_t)}')\n", | |
"print(f'avg time_for_l: {sum(time_for_l)/len(time_for_l)}')\n", | |
"print('test different way to concat the str in python Ends...')" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"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.9.5" | |
}, | |
"orig_nbformat": 4 | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment