TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to PYTHON

Python Dictionary String Key Performance

Time the dictionary get method with string keys of varying lengths. A short key is faster.
Dictionary, string keys. Often we think lookup time is constant, but many factors influence a dictionary's speed. For strings, each character must be hashed. So short keys are faster than long ones.
Example. Performance tests help us learn how collections really work. In this example, I create a dictionary with two keys. One is short, with just three letters (cat). And the second is longer with 22 characters in it.

Version 1: In this loop we do a get() for the first key. The get method internally computes a hash, but only needs to scan a short string.

Version 2: Here we call get() on the longer string. In each iteration, a hash code must be computed for the entire string.

Python program that times short, long string key lookups import time lookup = {"cat": 1, "anextremelylongstringkey": 2} print(time.time()) # Version 1: short string key. for i in range(0, 100000000): v = lookup.get("cat") print(time.time()) # Version 2: long string key. for i in range(0, 100000000): v = lookup.get("anextremelylongstringkey") print(time.time()) Output: PyPy 100 million iterations 1412466713.551 1412466715.161 Get short key = 1.61 s 1412466716.911 Get long key = 1.75 s Output: Python3 10 million iterations 1412467011.994969 1412467014.838859 Short = 2.84 s 1412467017.745185 Long = 2.91 s
Results. The short key is faster to look up in the dictionary. The hashing method in Python 3 is not free: it too requires some time. In large collections, or dictionaries with collisions, this time is less significant than in this test.

Thus: Using the shortest unique keys possible in a dictionary will improve performance.

Analysis: This is not a primary consideration, but performance tips like this one can help us develop better programs over time.

Dictionary
Summary. Dictionaries are one of the most important types in computer languages. They are helpful. Much of our information technology in the world uses hash codes and dictionaries. And with minimal string keys, performance is likely to improve.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf