TGTGInsighttelegram intelligenceLIVE / telegram public index
← Python Заметки

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

Резултати

Пронајдени 8 слични објави

Пребарај: #snowy

当前筛选 #snowy清除筛选
Pensive|

@PensivePost · Post #5840 · 29.06.2022 г., 21:19

Why am i so addled, i seek retort through this creation. Can't understand a thing, anymore. I just need Salvation What am i gonna gain, if anything, why do i need revelation. I do just fine without mass, getting back to isolation? Why is it so important, why look for identification? Is it the curiosity or the fear of seperation? There is inspiration, there is recreation, there is manipulation, sensation, violation & frustration but is there anything that's necessary? Is there any elevation? I know it bothers none but please, tell me, where am i heading to, what is my destination? #Snowy#review

Pensive|

@PensivePost · Post #5602 · 12.01.2022 г., 18:22

I mutilate myself, no matter what i do. Acting all spirit, i maim when I'm disregarded, when I'm passed over. Acting all foul isn't my cup of tea either, i maim again, i incapacitate either way. Being ignoble and or moral, both roads are noxious to me. What do i fancy then? You blemish me, your bygones. I can't condone it. I can't strand you either. You, your, it impedes me, candidly but won't gash you by disclaim, renounce.... Won't? Well, can't is more apt, can't bestow to soar your agony.... But about my torment? I have nowhere to go, no door left open, I'm stuck, stuck with you. I'll dwell, I'll persist, that's what I've been doing, that's what i can keep up with. No possibility, either at whim or reluctantly, i have to adhere, no recourse.... I'd be thrusting here, i don't know the stretch, the extent, maybe perpetuity. #review#Snowy

Pensive|

@PensivePost · Post #5589 · 07.01.2022 г., 12:05

Why am i so addled, i seek retort through this creation. Can't understand a thing, anymore. I just need Salvation What am i gonna gain, if anything, why do i need revelation. I do just fine without mass, getting back to isolation? Why is it so important, why look for identification? Is it the curiosity or the fear of seperation? There is inspiration, there is recreation, there is manipulation, sensation, violation & frustration but is there anything that's necessary? Is there any elevation? I know it bothers none but please, tell me, where am i heading to, what is my destination? #Snowy#review

Pensive|

@PensivePost · Post #5570 · 29.12.2021 г., 04:59

You were fire, i wasn't ice I was supposed to but it wasn't nice I was magma that was piquant Why was our bond so highly turbulent? The violent gesticulation of air around your flames Wasn't any good for my fever but i got used to the games We were bonded the way we weren't meant to be Was it destiny, i wondered or was it our will that was the key You're no good for me, i know, said she But it was too late for her to be set in the world again like a bird, free If we're meant to do it together, yes my love, we suffer like the way we suffered You'd be the flame of my candle that demolishes my wax, he uttered Afterall, diamonds dice diamonds, they are suited together Shouldn't be befuddled with types different but stay where they belong, forever #Snowy #review