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

Резултати

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

Пребарај: #towardszerowaste

当前筛选 #towardszerowaste清除筛选
NEA Singapore

@NEASingapore · Post #1016 · 30.03.2025 г., 01:03

Fashion doesn’t have to cost the earth! This International Day of Zero Waste puts the spotlight on fashion and textiles. Here’s how we can do our part to reduce textile waste: ✅ 𝗥𝗘𝗨𝗦𝗘 worn-out clothes for other purposes e.g. pillow covers, cleaning rags ✅ 𝗥𝗘𝗗𝗨𝗖𝗘 impulse purchases by checking if you already have something similar ✅ 𝗗𝗢𝗡𝗔𝗧𝗘 outgrown clothing #TowardsZeroWaste

NEA Singapore

@NEASingapore · Post #799 · 30.03.2024 г., 02:02

It’s the International Day of Zero Waste! If you’ve yet to embark on the journey #TowardsZeroWaste, here’s your sign to get started: go.gov.sg/towards-zero-waste

NEA Singapore

@NEASingapore · Post #544 · 02.05.2023 г., 00:01

It’s World Tuna Day! If you have leftover canned tuna (or any other canned fish), be sure to store it properly in a sealed container. This extends the lifespan of your leftovers and minimises food waste! #TowardsZeroWaste

NEA Singapore

@NEASingapore · Post #530 · 29.03.2023 г., 23:45

It’s the first International Day of Zero Waste, and a good opportunity to take stock of how far we’ve come in our journey #TowardsZeroWaste. If you’ve yet to get started on this journey, now’s the time! Tips at www.nea.gov.sg/our-services/towards-zero-waste

NEA Singapore

@NEASingapore · Post #488 · 15.01.2023 г., 02:01

Spring cleaning and getting rid of unwanted items, such as clothes or household items? If they are still in good condition, consider donating them! Discover what you can do with items that you no longer need here: go.gov.sg/nea-donate #TowardsZeroWaste

NEA Singapore

@NEASingapore · Post #167 · 16.03.2021 г., 02:00

Have you recycled your used PMD yet? From now until June 2021, you’ll be able to do so at collection points in residential areas. Check out where and when at https://www.kgs.com.sg/pmd-collection-program Aside from retrieving precious materials from its li-ion batteries, the metal parts can also be used in the manufacturing of other products. #TowardsZeroWaste

NEA Singapore

@NEASingapore · Post #165 · 09.03.2021 г., 03:00

Not sure what to do with your used Personal Mobility Device (PMD)? Recycle it! Aside from retrieving precious materials from its li-ion batteries, the metal parts can also be used in the manufacturing of other products. To make recycling PMDs more convenient, we are collaborating with KGS Pte Ltd to set up collection points at residential areas until June 2021. Check out where and when at https://www.kgs.com.sg/pmd-collection-program#TowardsZeroWaste

NEA Singapore

@NEASingapore · Post #575 · 26.06.2023 г., 09:30

Our actions today impact our world tomorrow, and it is up to us to make it a better one. Together, let’s work #TowardsZeroWaste and build a #SustainableSG. If you’re still not sure how to start, check out the activities and challenges at www.gogreen.gov.sg/programmes/

NEA Singapore

@NEASingapore · Post #723 · 16.12.2023 г., 01:00

As the festive clock ticks, let's not forget the ticking dates in our pantry! Place items nearing their expiry dates at the front of your shelves. This helps you remember to use them soon and reduce food wastage. #SayYEStoWasteLess#TowardsZeroWaste

NEA Singapore

@NEASingapore · Post #501 · 04.02.2023 г., 02:01

Have leftover items from your Lunar New Year festivities? Be sure to place food that is near expiry within view, and practise the “First to Expire, First Out” approach when organising your home pantry! #SayYEStoWasteLess#TowardsZeroWaste

123•••56
ПретходнаСтраница 1 од 6Следна