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

Резултати

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

Пребарај: #policartoon

当前筛选 #policartoon清除筛选
Social Justice Watch

@social_justice_watch · Post #17658 · 18.10.2022 г., 17:20

Nobody Back Then Knew Slavery Was Wrong Transcript and description of cartoon: link A #PoliCartoon by me and @hawkins_becky, who did an awesome job with historic settings and costumes! Help us make cartoons at patreon.com/barry. Or by retweeting! :-) source

Hashtags

Social Justice Watch

@social_justice_watch · Post #17657 · 18.10.2022 г., 17:19

What We Can Afford A #PoliCartoon by @kevinwmoore and I! Transcript: link Read more (and moore) cartoons!: link You can support these cartoons with retweets or at patreon.com/barry! source

Hashtags

Social Justice Watch

@social_justice_watch · Post #17656 · 18.10.2022 г., 17:18

Just because you're rich white and privileged and you have an super-elite job and you haven't been fired doesn't mean you can't claim quit and claim victimhood! Transcript of #PoliCartoon: link Support by retweeting! Or directly at patreon.com/barry. source

Hashtags

Social Justice Watch

@social_justice_watch · Post #17655 · 18.10.2022 г., 17:17

Civil Forfeiture! A #PoliCartoon by me and @kevinwmoore! A topic that no one has been asking for a cartoon about! Read more toons! link Support more toons! patreon.com/barrysource

Hashtags

Social Justice Watch

@social_justice_watch · Post #17650 · 18.10.2022 г., 17:13

A Brief Taxonomy of Pro-Lifers Transcript of #PoliCartoon: link Help us make more cartoons at patreon.com/barry. :-) Read lots more cartoons here! linksource

Hashtags

Social Justice Watch

@social_justice_watch · Post #17645 · 18.10.2022 г., 04:07

There's Never Been A Worse Time For Free Speech! A #policartoon by me and @hawkins_becky. Transcript: link Help us make more by retweeting, or at patreon.com/barry. A $1 pledge really helps! Lots more toons to read! linksource

Hashtags

Social Justice Watch

@social_justice_watch · Post #17642 · 18.10.2022 г., 04:00

The Secret Home Lives of Teachers A #PoliCartoon by me and @hawkins_becky. Transcript: link Lots more cartoons to read here: link Help us make more! patreon.com/barrysource

Hashtags

12
ПретходнаСтраница 1 од 2Следна