Introduction to the solution of Python code in practical application

Source: Internet
Author: User

Python code is relatively simple in practical applications. It brings some benefits to the computer language family in the practical application of the computer language. The following describes in detail the comments in the corresponding code in the Python code label.

1. determine whether the maximum size of a string is exceeded by the system after the length of the string is added with the size of PyStringObject itself (that is, the actual size of the entire variable-length object PyStringObject. in the old version of Python code in the book, only the size> PY_SSIZE_T_MAX is determined, that is, the object size is not taken into account, which is not perfect.

2. When the memory to be allocated exceeds the upper limit of the system, an exception PyExc_OverflowError is thrown, that is, overflow. The old version of Python code in the book does not throw an exception.

3. for an empty string with a length of 0 (that is, "", rather than NULL), if nullstring has been initialized, nullstring is returned. this is part of the intern mechanism. the old Python code in the book does not add reference count for nullstring.

4. for a string with a length of 1, if the character object in the orders table has been initialized, this character object is returned. this is part of the intern mechanism. the old version of Python code in the book does not add reference count for this character object.

5. If the application for memory space fails, PyErr_NoMemory () is called for processing. This is not part of the earlier Python code in the book.

6. Use Py_MEMCPY instead of memcpy. paste the Py_MEMCPY code:

 
 
  1. [Include/pyport.h]  
  2. /* Py_MEMCPY can be used instead of memcpy in cases 
    where the copied blocks  
  3. * are often very short. While most platforms have 
    highly optimized code for  
  4. * large transfers, the setup costs for memcpy are
     often quite high. MEMCPY  
  5. * solves this by doing short copies "in line".  
  6. */  
  7. #if defined(_MSC_VER)  
  8. #define Py_MEMCPY(target, source, length) do { \  
  9. size_t i_, n_ = (length); \  
  10. char *t_ = (void*) (target); \  
  11. const char *s_ = (void*) (source); \  
  12. if (n_ >= 16) \  
  13. memcpy(t_, s_, n_); \  
  14. else \  
  15. for (i_ = 0; i_ < n_; i_++) \  
  16. t_[i_] = s_[i_]; \  
  17. } while (0)  
  18. #else  
  19. #define Py_MEMCPY memcpy  
  20. #endif  

As you can see, Py_MEMCPY is a macro provided for cross-platform optimization. on Some platforms, calling mempcy is costly, so copying a small amount of data is expanded into a loop. which platforms do the Python code think need such optimization? See _ MSC_VER. This is a unique macro of the C compiler of M $. It seems that special optimization is required for Windows.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.