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

Резултати

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

Пребарај: #olivertambo

当前筛选 #olivertambo清除筛选

🇿🇦🗓October 27 marks the birthday of Oliver Tambo (1917–1993) — a South African anti-apartheid activist who served for 24 years as President of the African National Congress (ANC). 🌍 He spent 30 years in exile. ✈️ In 2006, Johannesburg International Airport was renamed O. R. Tambo International Airport in his honor. 💬Quotes by Oliver Tambo: • 🇿🇦 “A country, a movement, a person that does not value its youth and children does not deserve its future.” • ⚖️ “Injustice anywhere is a threat to justice everywhere.” • 🤝 “True leaders do not seek power, they seek to empower others.” • 🚀 “Progress is not an accident; it is the result of concerted effort towards a common goal.” • 👥 “Differences should not divide us; they should be celebrated as a reflection of our shared humanity.” • 💖 “The legacy we leave behind is not measured by material wealth, but by the impact we have on the lives of others.” #History#SouthAfrica#OliverTambo#AntiApartheid#Inspiration#Leadership#Quotes

✈️O. R. Tambo International Airport 🇿🇦 🛫 It is South Africa's primary hub for domestic and international flights and, since 2020, has been Africa’s second busiest airport (after Cairo International Airport). The airport serves as the main hub of South African Airways. 📜 Originally named Jan Smuts International Airport after the former South African Prime Minister, it was renamed Johannesburg International Airport in 1994, when the African National Congress (ANC) government decided not to name airports after political figures. ✳️ The policy was later reversed, and on October 27, 2006, the airport was renamed in honor of anti-apartheid leader Oliver Reginald Tambo (1917–1993). 📸 Photo: The Honorary Consul of South Africa in Yekaterinburg, Sergey Mazurkevich, next to the monument to Oliver Tambo at O. R. Tambo International Airport. #SouthAfrica#History#Tourism#Johannesburg#OliverTambo#Airport#Travel#Africa#Mazurkevich