<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Satyam Kumar Sinha</title>
    <description>The latest articles on DEV Community by Satyam Kumar Sinha (@satyam93sinha).</description>
    <link>https://dev.to/satyam93sinha</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F780865%2F29424905-0f02-4614-a77a-a230db1e5128.png</url>
      <title>DEV Community: Satyam Kumar Sinha</title>
      <link>https://dev.to/satyam93sinha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/satyam93sinha"/>
    <language>en</language>
    <item>
      <title>Which is faster dict() vs {} and why?</title>
      <dc:creator>Satyam Kumar Sinha</dc:creator>
      <pubDate>Tue, 25 Apr 2023 03:36:42 +0000</pubDate>
      <link>https://dev.to/satyam93sinha/performance-impact-for-vs-dict-104n</link>
      <guid>https://dev.to/satyam93sinha/performance-impact-for-vs-dict-104n</guid>
      <description>&lt;p&gt;dict() internally calls below opcodes:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R_ALXMIf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6sju5jbzks9zw1t2bhcr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R_ALXMIf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6sju5jbzks9zw1t2bhcr.png" alt="Image description" width="800" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;LOAD_NAME : Pushes the value associated with co_names[namei] onto the stack. So, whatever names have been declared are pushed to the interpreter stack through this OPCODE.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;PRECALL : Prefixes CALL. Logically this is a no op. It exists to enable effective specialization of calls. argc is the number of arguments as described in CALL.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;CALL : Calls a callable object with the number of arguments specified by argc(in PRECALL), including the named arguments specified by the preceding KW_NAMES, if any. On the stack are (in ascending order), either:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;NULL&lt;/li&gt;
&lt;li&gt;The callable&lt;/li&gt;
&lt;li&gt;The positional arguments&lt;/li&gt;
&lt;li&gt;The named arguments&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;or:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The callable&lt;/li&gt;
&lt;li&gt;self&lt;/li&gt;
&lt;li&gt;The remaining positional arguments&lt;/li&gt;
&lt;li&gt;The named arguments&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;argc is the total of the positional and named arguments, &lt;br&gt;
excluding self when a NULL is not present.&lt;/p&gt;

&lt;p&gt;CALL performs below steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;pops all arguments(key-value pairs) and the callable object(here, dict) off the stack&lt;/li&gt;
&lt;li&gt;calls the callable object(here, dict) with arguments if any.&lt;/li&gt;
&lt;li&gt;pushes the return value returned by the above callable object(here, dict)&lt;/li&gt;
&lt;li&gt;In brief, initialises the new object passing the keyword arguments to its initialisation method. Here, dict is called.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When we initialise the dict() with some key-value pairs as dict(a="A", b="B", c="C", d="D"):&lt;br&gt;
OPCODES image:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---SxPk35o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8nr6n5v7pojizbobe65k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---SxPk35o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8nr6n5v7pojizbobe65k.png" alt="Image description" width="800" height="342"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The process :&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;LOAD_NAME : loads the name of the object to be called and initialised, here it is dict as seen in the above image.&lt;/li&gt;
&lt;li&gt;LOAD_CONST: Pushes co_consts[consti] onto the stack. As we see in the above image, first 'A' is pushed then, 'B' and so on in the order they were defined in the code.&lt;/li&gt;
&lt;li&gt;KW_NAMES : Prefixes PRECALL. Stores a reference to 
co_consts[consti] into an internal variable for use by CALL. 
co_consts[consti] must be a tuple of strings. In other 
words, an internal variable(dictionary) is used to store all 
the key-value pairs defined/declared above and was pushed 
into the stack. These are thereafter used as arguments to 
CALL.&lt;/li&gt;
&lt;li&gt;PRECALL : argc is 4, the total number of arguments.&lt;/li&gt;
&lt;li&gt;CALL : 4 keyword arguments are used to make the call, 
initialise and create the dictionary object in this case.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt;&lt;br&gt;
    1. pushes the object to be created(dict) onto the stack using LOAD_NAME&lt;br&gt;
    2. pushes the key-value pairs if any using LOAD_CONST onto the stack as constant values.&lt;br&gt;
    3. pop out the key-value pairs off of the stack and create a dictionary to hold the keyword arguments to the function&lt;br&gt;
    4. The constructor for dict is called to make a new object&lt;br&gt;
    5. Initialise the new object by passing the keyword arguments to its initialisation method.&lt;br&gt;
    6. Resize the new dict and copy the key-value pairs into it from the keyword arguments.&lt;/p&gt;

&lt;p&gt;The same should be the case with (1, ) vs tuple(), [] vs list().&lt;/p&gt;

&lt;p&gt;References:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://docs.python.org/3/library/dis.html"&gt;https://docs.python.org/3/library/dis.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://doughellmann.com/posts/the-performance-impact-of-using-dict-instead-of-in-cpython-2-7-2/"&gt;https://doughellmann.com/posts/the-performance-impact-of-using-dict-instead-of-in-cpython-2-7-2/&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>python</category>
      <category>performance</category>
      <category>python3</category>
    </item>
  </channel>
</rss>
