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 759761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:38:10+00:00 2026-05-14T15:38:10+00:00

On my machine, the execution speed between d.clear() and d={} is over 100ns so

  • 0

On my machine, the execution speed between d.clear() and d={} is over 100ns so am curious why one would use one over the other.

import timeit

def timing():
    d = dict()

if __name__=='__main__':
    t = timeit.Timer('timing()', 'from __main__ import timing')
    print t.repeat()
  • 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-05-14T15:38:10+00:00Added an answer on May 14, 2026 at 3:38 pm

    d={} creates a new dictionary.

    d.clear() clears the dictionary.

    If you use d={}, then anything pointing to d will be pointing to the old d. This may introduce a bug.

    If you use d.clear(), then anything pointing at d will now point at the cleared dictionary, this may also introduce a bug, if that was not what you intended.

    Also, I don’t think d.clear() will (in CPython) free up memory taken up by d. For performance, CPython doesn’t take the memory away from dictionaries when you delete elements, as the usual use for dictionaries is building a big dictionary, and maybe pruning out a few elements. Reassigning memory (and making sure the hash table stays consistent) would take too long in most use cases. Instead, it fills the dictionary with turds (that’s the technical term on the mailing list), which indicate that an element used to be there but since got deleted. I’m not entirely sure if d.clear() does this though, but deleting all the keys one by one certainly does.

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

Sidebar

Related Questions

My Machine details : 32bit OS (win-7) , dual core , clock speed :
So at the suggestion of a colleague, I just tested the speed difference between
MySQL 5.1, Ubuntu 10.10 64bit, Linode virtual machine. All tables are InnoDB . One
I would like to compare different PHP code to know which one would be
I need to delete a file from the target machine before an execution of
Recently I installed node.js on my Windows 7 machine. On execution of JavaScript, I
I use the below command to delete some files after reboot the machine: MoveFileEx(PChar(File_Address),
I meet following exception when I copy the whole program directoy to another machine.
My machine is little-endian (Intel byte order). I need to read a binary file
My machine apparently won't draw vertex lists in pyglet. The following code renders two

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.