Keep your Python code efficient

Computer system architecture:

Why not GPU as our main computing unit?

Python data types:

Tuples

  • Tuples are immutable.
  • Tuples are cached in the Python runtime, which means there is no memory allocation request to the OS when creating a tuple.

Lists

  • Lists are dynamic and mutable.
  • When creating a list, the runtime request memory allocation from OS to allocate X size of memory.
Time of append and size of list with number of items in list

Dictionaries

  • Dictionary is a collection of key-value pairs. The key must be a hashable object.
  • The complexity of insertions is similar to the previous types: O(1). But it is also dependent on the hashing function used. If it is slow to computer that will affect the performance. Comments in dictobject C implementation has more details on this.
  • Hash is calculated for the key.
  • Few bits are taken from the search hash and search starts. If the no hash is matched is found KeyError is thrown. If there is a match: search_key and found_key will be compared: if the they are not equal, a hash collision happens.
  • How python resolves hash collision? Another search starts again but with different fraction of bits until the keys are equal (please check perturb function in the C implementation). Then it will return the value.
  • If we are doing update or insert, the item will be inserted or overwritten.

--

--

--

Software Engineer. Data nerd. Machine learning enthusiast.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Google Cloud Application Architecture Cont.

Common developer pitfalls when using soft skills and how to overcome them

5 Killer Automation Scripts in Python — Part-2

On Elasticsearch: Change of mind, change your distro

Old part of docker-compose

Week 01 — University Software Engineering Project

DAX - previous day

Let’s Build a Web App! Week 15 out of 30

Deep dive into AWS for developers | Part1

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Yasser Sinjab

Yasser Sinjab

Software Engineer. Data nerd. Machine learning enthusiast.

More from Medium

Useful functions in Python

MUTABLE / IMMUTABLE

Python Hard Parts (intermediate level)

Object-Oriented Programming in Plain English.