@LiveHealthy · Post #6940 · 24.01.2025 г., 00:52
#happiness
Hashtags
TGINSIGHT SIMILAR POSTS
Изворен канал @pythonotes · Post #32 · 7 фев.
Скорее всего уже слышали, что складывать строки через + это плохая практика. Падение производительности, и всё такое. Без лишних слов, давайте измерять: from timeit import timeit def t1(): # складываем 10 строк через + из переменной t = 'text' for _ in range(1000): s = t + t + t + t + t + t + t + t + t def t2(): # склеиваем список строк через метод join arr = ['text'] * 10 for _ in range(1000): s = ''.join(arr) def t3(): # складываем через + но не из переменной а непосредственно инлайн объекты for _ in range(1000): s = 'text' + 'text' + 'text' + ... # всего 10 раз Теперь каждую строку склейки запустим по 10М раз >>> timeit(t1, number=10000) 0.21951690399964718 >>> timeit(t2, number=10000) 1.4978306379998685 >>> timeit(t3, number=10000) 0.2213820789993406 Хм, а нам говорили что через "+" это плохо и медленно ))) 😁 Тут стоит учитывать, что речь идёт о склейке множества длинных строк. Давайте изменим условия: def t4(): t = 'text'*100 for _ in range(1000): s = t + t + t + t + t + t + t + t + t def t5(): arr = ['text'*100] * 10 for _ in range(1000): s = ''.join(arr) def t6(): for _ in range(1000): s = 'text'*100 + 'text'*100 + ... # всего 10 раз >>> timeit(t4, number=10000) 12.795130728000004 >>> timeit(t5, number=10000) 2.642637542999182 >>> timeit(t6, number=10000) 0.2184546610005782 Вот, уже другой разговор, сразу видна разница, в среднем в 6 раз. Но погодите, почему последний тест t6() по скорости такой же как и t3()? Ведь строки теперь в 100 раз длиннее! Это вопросы оптимизации кода, какие простые изменения ускоряют или замедляют выполнение программы. Мы столкнулись с примером обхода обращения к переменной. Например, именно так работает директива #define в С++, во время компиляции подставляя значение переменной вместо ссылки на неё. В Python это тоже работает, но часто ли вы сможете встретить такой способ работы со строками? К сожалению, способ почти только теоретический. В целом, тесты показали то, что мы хотели. Делаем выводы самостоятельно. Полный листинг 🌍 #tricks
Пребарај: #happiness
@LiveHealthy · Post #6940 · 24.01.2025 г., 00:52
#happiness
Hashtags
@QuietWorld · Post #6796 · 26.10.2019 г., 08:30
The first recipe to happiness is to avoid lengthy meditation of the past. 🍃 #happiness@quietworld🍃
Hashtags
@QuietWorld · Post #6787 · 25.10.2019 г., 08:30
Happiness will never come to those who never appreciate what they already have. #happiness@quietworld🍃
Hashtags
@QuietWorld · Post #6756 · 20.10.2019 г., 23:30
Recall it as often as you wish, a happy memory never wears out. —Libbie Fudim #happiness@quietworld🍃
Hashtags
@QuietWorld · Post #6738 · 18.10.2019 г., 23:30
The word 'happiness' would lose its meaning if it were not balanced by sadness. – Carl Jung #happiness@quietworld🍃
Hashtags
@talks · Post #286 · 26.05.2018 г., 11:46
#happiness
Hashtags
@QuietWorld · Post #6772 · 23.10.2019 г., 08:30
Thousands of candles can be lighted from a single candle & the life of the candle will not be shortened. Happiness never decreased by being shared. – Buddha – but of course this doesn't apply to all kinds of happiness though, for example; I would surely be less happy if I were to share a husband with someone🧐 #happiness#thoughts @quietworld🍃
Hashtags
@QuietWorld · Post #6617 · 28.09.2019 г., 03:58
Happiness is not the absence of problems, but the ability to deal with them. #happiness#quotes @quietworld🍃
Hashtags
@QuietWorld · Post #6797 · 26.10.2019 г., 13:00
Happiness doesn't come from doing easy work but from the afterglow of satisfaction that comes after the achievement of a difficult task that demanded our best. #happiness#motivation @quietworld🍃
Hashtags
@QuietWorld · Post #7474 · 08.07.2020 г., 04:02
My ex-schoolmates, ex-collegemates, ex-everything on social media: • getting married • celebrating their first pregnancy • flexing new expensive car • flexing their lover • flexing their expensive pets 😍 Me: • happily flexing the stray kittens I adopted 🥰 The amount of happiness, is same 🥰 Just make sure you know what actually makes you happy 😄💐 What makes you happy? Comment down below.😉 #thoughts#life#happiness @quietworld🍃
Hashtags
@gumiho_wallpaper · Post #866 · 21.10.2024 г., 00:10
#Kdrama #Happiness
Hashtags
@QuietWorld · Post #6739 · 19.10.2019 г., 06:30
"Everyone wants happiness, nobody wants pains, but you can't have the rainbow, without a little rain." #happiness#selfreminder @quietworld🍃
Hashtags