Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8116975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:59:53+00:00 2026-06-06T03:59:53+00:00

On this page , I see something interesting: Note that there is a fast-path

  • 0

On this page, I see something interesting:

Note that there is a fast-path for dicts that (in practice) only deal with str keys; this doesn’t affect the algorithmic complexity, but it can significantly affect the constant factors: how quickly a typical program finishes.

So what does it exactly mean?

Does it mean using string as the key is always faster?

If yes, why?

Update:

Thanks for the suggestions about optimization! But I’m actually more interested in the plain truth, than whether or when we should do optimization.

Update 2:

Thanks for the great answers, I’ll cite the content from the link provided by @DaveWebb here:

”
…

ma_lookup is initially set to the lookdict_string function (renamed to lookdict_unicode in 3.0), which assumes that both the keys in the dictionary and the key being searched for are standard PyStringObject’s. It is then able to make a couple of optimiziations, such as mitigating various error checks, since string-to-string comparison never raise exceptions. There is also no need for rich object comparisons either, which means we avoid calling PyObject_RichCompareBool, and always use _PyString_Eq directly.

…
“

Also, for the experiment numbers, I think the size of the difference will be even bigger if there is no int-to-string conversion

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:59:55+00:00Added an answer on June 6, 2026 at 3:59 am

    The C code that underlies the Python dict is optimisted for String keys. You can read about this here (and in the book the blog refers to).

    If the Python runtime knows your dict only contains string keys it can do things such as not cater for errors that won’t happen with a string to string comparison and ignore the rich comparison operators. This will make the common case of the string key only dict a little faster. (Update: timing shows it to be more than a little.)

    However, it is unlikely that this would make a significant change to the run time of most Python programs. Only worry about this optimisation if you have measured and found dict lookups to be a bottleneck in your code. As the famous quote says, “Premature optimization is the root of all evil.”

    The only way to see how much faster things really are, is to time them:

    >>> timeit.timeit('a["500"]','a ={}\nfor i in range(1000): a[str(i)] = i')
    0.06659698486328125
    >>> timeit.timeit('a[500]','a ={}\nfor i in range(1000): a[i] = i')
    0.09005999565124512
    

    So using string keys is about 30% faster even compared to int keys, and I have to admit I was surprised at the size of the difference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When you go to this page; http://www.ctuchicago.com/registration/login.php you will see the form that I
See section /* Common Classes */ of this page. http://webdesign.about.com/od/css/a/master_stylesht_2.htm are these css classes
On this page: http://www.colorz.fr/#!/work/ You can see the image scrolls into/out of the direction
So I have this page here: http://www.eminentmedia.com/development/powercity/ As you can see when you mouse
Im trying to use this page slider jquery plugin, you can see the demo
Possible Duplicate: Checkbox Stays Checked on Page Refresh Please see the image carefully. This
This is how I see my screen the when the page loads. But when
See code below: I want Page 1 to alert #testing... This works in C
For a page in our app there is a certain number of customers that
Hopefully someone can see something which I've missed out or something, because this is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.