SlideShare ist ein Scribd-Unternehmen logo
1 von 10
How Python internals
affect your code
Martijn Pieters
Abstractions galore
Programming is all about abstractions.
All non-trivial abstractions, to some
degree, are leaky.
Joel Spolsky’s Law of Leaky Abstractions
CPython translates your Python code into
machine instructions.
Small integers are singletons
● Everything is an object
● Integers are used all over the place
● Small integers are singletons, there is only
one copy of each small number
● -5 through to 256 are all cached like this
Small integers are singletons
Beginners confuse is with ==
Identity is not the same as equality
Use is only for objects that you know to be
singletons, always, like None.
Some strings are interned
Interning: reusing a singleton copy on demand
● All identifiers are interned
● Many string literals are interned
Comparing pointers in C is so much faster than
comparing the contents
Everything is a dictionary -> lots of comparing
When to intern too
Python code can use the intern() function
Use this together with is identity testing
When:
● Large numbers of strings
● Lots of dictionary access or other equality
tests
Peephole optimisations
Peephole optimizer is part of the compiler
● Expressions are simplified
● Some mutable objects are replaced with
immutables
Python will precalculate expressions:
● Numeric calculationsten_days = 10 * 24
* 60 * 60
● Sequencesdefault_retval = (None,) *
5
taunt = 'neener' * 3
Expressions are simplified
Mutables replaced by immutables
Membership tests against a literal:
if foo in {'ham', 'spam', 'eggs'}
The set() becomes a frozenset()
Introspection and Dissasembly
Demo of object attributes and the dis module

Weitere ähnliche Inhalte

Was ist angesagt?

Raspberry using Python Session 1
Raspberry using Python Session 1Raspberry using Python Session 1
Raspberry using Python Session 1Mohamed Abd Ela'al
 
Perl::Lint - Yet Another Perl Source Code Linter
Perl::Lint - Yet Another Perl Source Code LinterPerl::Lint - Yet Another Perl Source Code Linter
Perl::Lint - Yet Another Perl Source Code Lintermoznion
 
PART 9 - Python Tutorial | While Loop In Python With Examples
PART 9 - Python Tutorial | While Loop In Python With ExamplesPART 9 - Python Tutorial | While Loop In Python With Examples
PART 9 - Python Tutorial | While Loop In Python With ExamplesShivam Mitra
 
Static analysis for perl
Static analysis for perlStatic analysis for perl
Static analysis for perlmoznion
 
Emnlp読み会@2017 02-15
Emnlp読み会@2017 02-15Emnlp読み会@2017 02-15
Emnlp読み会@2017 02-15sekizawayuuki
 
Python presentation
Python presentationPython presentation
Python presentationgaganapponix
 
Kotlin & Arrow the functional way
Kotlin & Arrow the functional wayKotlin & Arrow the functional way
Kotlin & Arrow the functional wayThoughtworks
 
Python presentation by Monu Sharma
Python presentation by Monu SharmaPython presentation by Monu Sharma
Python presentation by Monu SharmaMayank Sharma
 
The role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler designThe role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler designSadia Akter
 
Pharo: A Reflective System
Pharo: A Reflective SystemPharo: A Reflective System
Pharo: A Reflective SystemMarcus Denker
 
Part 2 - Python Tutorial | Introduction to Lists
Part 2 - Python Tutorial | Introduction to ListsPart 2 - Python Tutorial | Introduction to Lists
Part 2 - Python Tutorial | Introduction to ListsShivam Mitra
 
About Tokens and Lexemes
About Tokens and LexemesAbout Tokens and Lexemes
About Tokens and LexemesBen Scholzen
 
C# 4.0 and .NET 4.0
C# 4.0 and .NET 4.0C# 4.0 and .NET 4.0
C# 4.0 and .NET 4.0Buu Nguyen
 
Antlr part1 introduction
Antlr part1 introductionAntlr part1 introduction
Antlr part1 introductionMorteza Zakeri
 
Getting started with scala cats
Getting started with scala catsGetting started with scala cats
Getting started with scala catsKnoldus Inc.
 
PART 8 - Python Tutorial | User Input In Python With Examples
PART 8 - Python Tutorial | User Input In Python With ExamplesPART 8 - Python Tutorial | User Input In Python With Examples
PART 8 - Python Tutorial | User Input In Python With ExamplesShivam Mitra
 

Was ist angesagt? (20)

Raspberry using Python Session 1
Raspberry using Python Session 1Raspberry using Python Session 1
Raspberry using Python Session 1
 
Perl::Lint - Yet Another Perl Source Code Linter
Perl::Lint - Yet Another Perl Source Code LinterPerl::Lint - Yet Another Perl Source Code Linter
Perl::Lint - Yet Another Perl Source Code Linter
 
Unit1 pps
Unit1 ppsUnit1 pps
Unit1 pps
 
PART 9 - Python Tutorial | While Loop In Python With Examples
PART 9 - Python Tutorial | While Loop In Python With ExamplesPART 9 - Python Tutorial | While Loop In Python With Examples
PART 9 - Python Tutorial | While Loop In Python With Examples
 
Static analysis for perl
Static analysis for perlStatic analysis for perl
Static analysis for perl
 
Emnlp読み会@2017 02-15
Emnlp読み会@2017 02-15Emnlp読み会@2017 02-15
Emnlp読み会@2017 02-15
 
Python presentation
Python presentationPython presentation
Python presentation
 
Kotlin & Arrow the functional way
Kotlin & Arrow the functional wayKotlin & Arrow the functional way
Kotlin & Arrow the functional way
 
Python presentation by Monu Sharma
Python presentation by Monu SharmaPython presentation by Monu Sharma
Python presentation by Monu Sharma
 
The role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler designThe role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler design
 
Pharo: A Reflective System
Pharo: A Reflective SystemPharo: A Reflective System
Pharo: A Reflective System
 
Part 2 - Python Tutorial | Introduction to Lists
Part 2 - Python Tutorial | Introduction to ListsPart 2 - Python Tutorial | Introduction to Lists
Part 2 - Python Tutorial | Introduction to Lists
 
About Tokens and Lexemes
About Tokens and LexemesAbout Tokens and Lexemes
About Tokens and Lexemes
 
C# 4.0 and .NET 4.0
C# 4.0 and .NET 4.0C# 4.0 and .NET 4.0
C# 4.0 and .NET 4.0
 
Antlr part1 introduction
Antlr part1 introductionAntlr part1 introduction
Antlr part1 introduction
 
Getting started with scala cats
Getting started with scala catsGetting started with scala cats
Getting started with scala cats
 
Role-of-lexical-analysis
Role-of-lexical-analysisRole-of-lexical-analysis
Role-of-lexical-analysis
 
Compiler lecture 07
Compiler lecture 07Compiler lecture 07
Compiler lecture 07
 
Lexical Analyzers and Parsers
Lexical Analyzers and ParsersLexical Analyzers and Parsers
Lexical Analyzers and Parsers
 
PART 8 - Python Tutorial | User Input In Python With Examples
PART 8 - Python Tutorial | User Input In Python With ExamplesPART 8 - Python Tutorial | User Input In Python With Examples
PART 8 - Python Tutorial | User Input In Python With Examples
 

Andere mochten auch

Advanced templates
Advanced templatesAdvanced templates
Advanced templatesSencha
 
Programación matématica (optimización) con Python - Ingeniería Química - PyConES
Programación matématica (optimización) con Python - Ingeniería Química - PyConESProgramación matématica (optimización) con Python - Ingeniería Química - PyConES
Programación matématica (optimización) con Python - Ingeniería Química - PyConESCAChemE
 
Migrating to Puppet 4.0
Migrating to Puppet 4.0Migrating to Puppet 4.0
Migrating to Puppet 4.0Puppet
 
Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg...
Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg...Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg...
Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg...Puppet
 
Optimizacion con Python (Pyomo vs GAMS vs AMPL)
Optimizacion con Python (Pyomo vs GAMS vs AMPL)Optimizacion con Python (Pyomo vs GAMS vs AMPL)
Optimizacion con Python (Pyomo vs GAMS vs AMPL)CAChemE
 
Faster Python Programs Through Optimization by Dr.-Ing Mike Muller
Faster Python Programs Through Optimization by Dr.-Ing Mike MullerFaster Python Programs Through Optimization by Dr.-Ing Mike Muller
Faster Python Programs Through Optimization by Dr.-Ing Mike MullerPyData
 
25 php interview questions – codementor
25 php interview questions – codementor25 php interview questions – codementor
25 php interview questions – codementorArc & Codementor
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental PrinciplesIntro C# Book
 

Andere mochten auch (9)

Advanced templates
Advanced templatesAdvanced templates
Advanced templates
 
Programación matématica (optimización) con Python - Ingeniería Química - PyConES
Programación matématica (optimización) con Python - Ingeniería Química - PyConESProgramación matématica (optimización) con Python - Ingeniería Química - PyConES
Programación matématica (optimización) con Python - Ingeniería Química - PyConES
 
Migrating to Puppet 4.0
Migrating to Puppet 4.0Migrating to Puppet 4.0
Migrating to Puppet 4.0
 
Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg...
Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg...Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg...
Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg...
 
Optimizacion con Python (Pyomo vs GAMS vs AMPL)
Optimizacion con Python (Pyomo vs GAMS vs AMPL)Optimizacion con Python (Pyomo vs GAMS vs AMPL)
Optimizacion con Python (Pyomo vs GAMS vs AMPL)
 
Python Workshop
Python  Workshop Python  Workshop
Python Workshop
 
Faster Python Programs Through Optimization by Dr.-Ing Mike Muller
Faster Python Programs Through Optimization by Dr.-Ing Mike MullerFaster Python Programs Through Optimization by Dr.-Ing Mike Muller
Faster Python Programs Through Optimization by Dr.-Ing Mike Muller
 
25 php interview questions – codementor
25 php interview questions – codementor25 php interview questions – codementor
25 php interview questions – codementor
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 

Ähnlich wie How Python Internals Impact Code Performance

Py-Slides- easuajsjsjejejjwlqpqpqpp1.pdf
Py-Slides- easuajsjsjejejjwlqpqpqpp1.pdfPy-Slides- easuajsjsjejejjwlqpqpqpp1.pdf
Py-Slides- easuajsjsjejejjwlqpqpqpp1.pdfshetoooelshitany74
 
Python for kids - 1.pptx
Python for kids - 1.pptxPython for kids - 1.pptx
Python for kids - 1.pptxsanyam1427
 
Python Programming.pdf
Python Programming.pdfPython Programming.pdf
Python Programming.pdfssuser9a6ca1
 
Python - Introduction
Python - IntroductionPython - Introduction
Python - Introductionstn_tkiller
 
Tutorial on-python-programming
Tutorial on-python-programmingTutorial on-python-programming
Tutorial on-python-programmingChetan Giridhar
 
Python_Introduction&DataType.pptx
Python_Introduction&DataType.pptxPython_Introduction&DataType.pptx
Python_Introduction&DataType.pptxHaythamBarakeh1
 
web programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Malothweb programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh MalothBhavsingh Maloth
 
Introduction to Python Unit -1 Part .pdf
Introduction to Python Unit -1 Part .pdfIntroduction to Python Unit -1 Part .pdf
Introduction to Python Unit -1 Part .pdfVaibhavKumarSinghkal
 
Python (3).pdf
Python (3).pdfPython (3).pdf
Python (3).pdfsamiwaris2
 
Python presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, BiharPython presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, BiharUttamKumar617567
 
python classes in thane
python classes in thanepython classes in thane
python classes in thanefaizrashid1995
 

Ähnlich wie How Python Internals Impact Code Performance (20)

Python Programming 1.pptx
Python Programming 1.pptxPython Programming 1.pptx
Python Programming 1.pptx
 
17575602.ppt
17575602.ppt17575602.ppt
17575602.ppt
 
Py-Slides- easuajsjsjejejjwlqpqpqpp1.pdf
Py-Slides- easuajsjsjejejjwlqpqpqpp1.pdfPy-Slides- easuajsjsjejejjwlqpqpqpp1.pdf
Py-Slides- easuajsjsjejejjwlqpqpqpp1.pdf
 
Introduction to Python Programming
Introduction to Python Programming Introduction to Python Programming
Introduction to Python Programming
 
Python for kids - 1.pptx
Python for kids - 1.pptxPython for kids - 1.pptx
Python for kids - 1.pptx
 
Python Programming.pdf
Python Programming.pdfPython Programming.pdf
Python Programming.pdf
 
Python - Introduction
Python - IntroductionPython - Introduction
Python - Introduction
 
python-ppt.ppt
python-ppt.pptpython-ppt.ppt
python-ppt.ppt
 
python-ppt.ppt
python-ppt.pptpython-ppt.ppt
python-ppt.ppt
 
Tutorial on-python-programming
Tutorial on-python-programmingTutorial on-python-programming
Tutorial on-python-programming
 
Python_Introduction&DataType.pptx
Python_Introduction&DataType.pptxPython_Introduction&DataType.pptx
Python_Introduction&DataType.pptx
 
web programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Malothweb programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Maloth
 
Introduction to Python Unit -1 Part .pdf
Introduction to Python Unit -1 Part .pdfIntroduction to Python Unit -1 Part .pdf
Introduction to Python Unit -1 Part .pdf
 
Python (3).pdf
Python (3).pdfPython (3).pdf
Python (3).pdf
 
Python presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, BiharPython presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, Bihar
 
Python intro
Python introPython intro
Python intro
 
python classes in thane
python classes in thanepython classes in thane
python classes in thane
 
Class_X_PYTHON_J.pdf
Class_X_PYTHON_J.pdfClass_X_PYTHON_J.pdf
Class_X_PYTHON_J.pdf
 
Python 01.pptx
Python 01.pptxPython 01.pptx
Python 01.pptx
 
Python basics
Python basicsPython basics
Python basics
 

Mehr von Arc & Codementor

Remote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of Arc
Remote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of ArcRemote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of Arc
Remote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of ArcArc & Codementor
 
Introduction to Python for Data Science
Introduction to Python for Data ScienceIntroduction to Python for Data Science
Introduction to Python for Data ScienceArc & Codementor
 
20 iOS developer interview questions
20 iOS developer interview questions20 iOS developer interview questions
20 iOS developer interview questionsArc & Codementor
 
29 Essential AngularJS Interview Questions
29 Essential AngularJS Interview Questions29 Essential AngularJS Interview Questions
29 Essential AngularJS Interview QuestionsArc & Codementor
 
37 Java Interview Questions
37 Java Interview Questions37 Java Interview Questions
37 Java Interview QuestionsArc & Codementor
 
21 Essential JavaScript Interview Questions
21 Essential JavaScript Interview Questions21 Essential JavaScript Interview Questions
21 Essential JavaScript Interview QuestionsArc & Codementor
 
Top 10 Programming Languages in 2015
Top 10 Programming Languages in 2015Top 10 Programming Languages in 2015
Top 10 Programming Languages in 2015Arc & Codementor
 
How to Build Your App from Scratch
How to Build Your App from ScratchHow to Build Your App from Scratch
How to Build Your App from ScratchArc & Codementor
 
Angular meteor for angular devs
Angular meteor for angular devsAngular meteor for angular devs
Angular meteor for angular devsArc & Codementor
 
Introduction to Tmux - Codementor Tmux Office Hours Part 1
Introduction to Tmux - Codementor Tmux Office Hours Part 1Introduction to Tmux - Codementor Tmux Office Hours Part 1
Introduction to Tmux - Codementor Tmux Office Hours Part 1Arc & Codementor
 
Codementor Office Hours with Eric Chiang: Stdin, Stdout: pup, Go, and life at...
Codementor Office Hours with Eric Chiang: Stdin, Stdout: pup, Go, and life at...Codementor Office Hours with Eric Chiang: Stdin, Stdout: pup, Go, and life at...
Codementor Office Hours with Eric Chiang: Stdin, Stdout: pup, Go, and life at...Arc & Codementor
 
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...Arc & Codementor
 

Mehr von Arc & Codementor (13)

Remote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of Arc
Remote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of ArcRemote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of Arc
Remote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of Arc
 
Introduction to Python for Data Science
Introduction to Python for Data ScienceIntroduction to Python for Data Science
Introduction to Python for Data Science
 
20 iOS developer interview questions
20 iOS developer interview questions20 iOS developer interview questions
20 iOS developer interview questions
 
29 Essential AngularJS Interview Questions
29 Essential AngularJS Interview Questions29 Essential AngularJS Interview Questions
29 Essential AngularJS Interview Questions
 
37 Java Interview Questions
37 Java Interview Questions37 Java Interview Questions
37 Java Interview Questions
 
21 Essential JavaScript Interview Questions
21 Essential JavaScript Interview Questions21 Essential JavaScript Interview Questions
21 Essential JavaScript Interview Questions
 
Top 10 Programming Languages in 2015
Top 10 Programming Languages in 2015Top 10 Programming Languages in 2015
Top 10 Programming Languages in 2015
 
How to Build Your App from Scratch
How to Build Your App from ScratchHow to Build Your App from Scratch
How to Build Your App from Scratch
 
Angular meteor for angular devs
Angular meteor for angular devsAngular meteor for angular devs
Angular meteor for angular devs
 
Tmux tips and_tricks
Tmux tips and_tricksTmux tips and_tricks
Tmux tips and_tricks
 
Introduction to Tmux - Codementor Tmux Office Hours Part 1
Introduction to Tmux - Codementor Tmux Office Hours Part 1Introduction to Tmux - Codementor Tmux Office Hours Part 1
Introduction to Tmux - Codementor Tmux Office Hours Part 1
 
Codementor Office Hours with Eric Chiang: Stdin, Stdout: pup, Go, and life at...
Codementor Office Hours with Eric Chiang: Stdin, Stdout: pup, Go, and life at...Codementor Office Hours with Eric Chiang: Stdin, Stdout: pup, Go, and life at...
Codementor Office Hours with Eric Chiang: Stdin, Stdout: pup, Go, and life at...
 
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
 

Kürzlich hochgeladen

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 

Kürzlich hochgeladen (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 

How Python Internals Impact Code Performance

  • 1. How Python internals affect your code Martijn Pieters
  • 2. Abstractions galore Programming is all about abstractions. All non-trivial abstractions, to some degree, are leaky. Joel Spolsky’s Law of Leaky Abstractions CPython translates your Python code into machine instructions.
  • 3. Small integers are singletons ● Everything is an object ● Integers are used all over the place ● Small integers are singletons, there is only one copy of each small number ● -5 through to 256 are all cached like this
  • 4. Small integers are singletons Beginners confuse is with == Identity is not the same as equality Use is only for objects that you know to be singletons, always, like None.
  • 5. Some strings are interned Interning: reusing a singleton copy on demand ● All identifiers are interned ● Many string literals are interned Comparing pointers in C is so much faster than comparing the contents Everything is a dictionary -> lots of comparing
  • 6. When to intern too Python code can use the intern() function Use this together with is identity testing When: ● Large numbers of strings ● Lots of dictionary access or other equality tests
  • 7. Peephole optimisations Peephole optimizer is part of the compiler ● Expressions are simplified ● Some mutable objects are replaced with immutables
  • 8. Python will precalculate expressions: ● Numeric calculationsten_days = 10 * 24 * 60 * 60 ● Sequencesdefault_retval = (None,) * 5 taunt = 'neener' * 3 Expressions are simplified
  • 9. Mutables replaced by immutables Membership tests against a literal: if foo in {'ham', 'spam', 'eggs'} The set() becomes a frozenset()
  • 10. Introspection and Dissasembly Demo of object attributes and the dis module

Hinweis der Redaktion

  1. Everything in Python is an object. This includes integers. And integers are used all over the place; ask for the length of something? The result is an integer object. Want to index into a list? You have to create an integer object for that. Etc. To save on creating and destroying too many objects, the most commonly used integer values are cached; you always get the *same, single copy* of such integers. That’s fine, because these objects are immutable, sharing a single copy carries no risk of corruption. The values -5 through to 256 are all singletons.
  2. Why does this matter? Beginners may get confused between `is` and `==` , between identity testing and equality testing. The former tests if two object references are pointing to the same object, while the latter tests if two objects contain the same value. Since these small integers are all singletons, `is` *always works for these*; if you have a value 42 in one place and another 42 in another, they are always the same object. But for larger integers this fails and confusion ensues. Demo: foo = 6 * 8 bar = 40 + 2 foo == bar foo is bar
  3. Some strings are cached too; they are *interned*. Interning means that the interpreter will explicitly reuse a singleton version of strings. Small integers are reused by the constructor, so *always*. Interned strings are only reused explicitly, the interpreter makes a decision to intern a string when creating one. In Python, all identifiers (names in your program, including attributes on objects) are interned. Creating a class? Then your class name, all attributes including the method names and all arguments and local names in the functions are all interned. When you create a string literal, so a string value in quotes, and the value *looks enough* like an identifier, then it is interned too. Why was this done? Namespaces in Python are dictionaries. A lookup in a dictionary is fast thanks to hashing, but always requires an equality test on the key too, because hashing into the dictionary table is not unique. Python code does **loads** of namespace lookups, all the time. Python first tests if the C pointers are the same, an identity test, because that is so much faster than a string comparison, character by character.
  4. If your program has to handle a *lot* of text lookups (in dictionaries, for example), it could be advantageous to use interning too. You can use the built-in function `intern()` to produce singletons; apply it judiciously to your dictionary keys and anywhere you want to test for those keys.
  5. The peephole optimizer is part of the Python compiler. It applies a few tricks to your code. The Python compiler stores not just code, but also constants; integers, strings, tuples, anything that is immutable and defined with your code is stored as a constant for quick and easy access. To aid in this: Expressions are simplified Some mutable objects are replaced with immutables (Next slides elaborate)
  6. Mutable literals in membership tests are replaced with an immutable variant list -> tuple set -> frozenset Membership testing in sets is faster than using a tuple, make use of it!
  7. Demo showing constants in introspection