body, html { width: 100%; height: 100%; } body{ padding-top:100px; } body, h1, h2, h3, h4, h5, h6 { font-family: "Lato","Helvetica Neue",Helvetica,Arial,sans-serif; font-weight: 700; } #content{ min-height: 600px; } #banner_image { padding-bottom: 50px; text-align: center; color: #f8f8f8; background: url(../img/intro-bg_1.jpg) no-repeat center center; background-size: cover; } #banner_content { position: relative; padding-top: 6%; padding-bottom: 6%; margin-top: 12%; margin-bottom: 12%; background-color: rgba(0, 0, 0, 0.7); max-width: 660px; } #item_list { padding-top: 50px; } /*This code ensures that when we navigate to a particular section of a page, the section does not get lost behind the header*/ #cameras:before, #watches:before, #shirts:before{ display: block; content: " "; margin-top: -85px; height: 85px; visibility: hidden; } #login-panel .panel-footer{ font-weight:normal; } footer { padding: 10px 0; background-color: #101010; color:#9d9d9d; bottom: 0; width: 100%; } .red{ color:red; } #settings-container{ margin-bottom:10px; } .navbar-brand{ font-size:20px; } @media(max-width:768px) { #banner_content { padding-bottom: 15%; } footer{ text-align:left; } } .remove_item_link{ color:#0000ff; }

Why C and C++ runs faster than Python, Perl and Ruby

This answer has the potential to get overly complex, so I'll try and keep it simple and concentrate on the “big picture”.
It basically boils down. To the difference between a statically typed (like C/C++) vs dynamically typed languages (like Python, Perl and Ruby).
In a static language like C/C++ the software being developed uses a closed system of types. The compiler has complete knowledge of the types it is required to support and can therefore perform extensive optimizations without any risk of the code not working.
As a quick example suppose we are adding 2 unsigned 32 bit ints. The c compiler can inline this into a couple of machine instructions, totally avoiding the need to generate a jump instruction and also eliminating the need to protect the register state on the stack.
Python can't do that kind of optimization. Since a function in Python is free to return literally any type imaginable or nothing at all
The return type of my two functions are unknowable when the byte codes are compiled. So Python and any similarly dynamic language needs to call both functions before it can evaluate which of multiple possible +operators is most appropriate for the types involved. When it finally resolves the address of the + operators, it makes a function call incurring the typical stack overhead.
In a dynamically typed language like Python the compiled bytecodes are run inside the python interpretter. The “compiling” is more a translation from text to binary this reduces the amout of i/o and eliminates the repeated parsing of Python sources so the code can load faster.
Now suppose I take my wonderful C++ function that adds 2 ints together and package as a library and distribute it. Then someone tries to use the library but they need to add 64 bit doubles. How does my code handle that? It doesn't it, doesn't have to. The errant user will immediately get a compiler error because the c++ compiler is very pedantic about types matching.
But lets say the errant user is very determined and they decided to edit the prototype in the header file I provided thinking they've found the loophole what happens next is they are able to compile but they get a linker error so they are still thwarted.
Why did I mention all that stuff about compilers, linkers and libraries? To point out that c++ (and static languages in general) forgo some convenience for the end user. Instead of making it work the way the user needed we throw up roadblocks to prevent incorrect usage.
But by limiting the scope of what the code is required to handle amazing performance gains are possible. Essentially with C++ you get the advantages of high level languages but with the speed of “bare metal” machine coding without the portability issues.
Depending on the application and the programmers attitude this tradeoff of convenience vs performance may or may not make sense. Also there really is such a thing as throwaway code, and in that case getting something done quickly in Python is a clear win over trying to do the same thing in C++.
It really boils down to knowing your tools, and selecting the right tool for the right job.
There are also ways to improve Python performance but these typically involve goving up some flexibility. For more in this google Cython.
Post a Comment

Amazing Facts about Programming

Amazing Facts about programming  The first programmer was a lady named Ada Lovelac . She was a writer and gifted mathematician...

Popular Tutorials