python code block caching mechanism, small data pool mechanism.

Caching the same code block

A module in python, a function, a class, a file, etc. is a block of code.

Mechanism content: the Python commands executed during the same block of code to initialize the object, checks whether the value already exists, if present, it will be reused. In other words: When you perform the same block, encountered command to initialize the object, he will be initialized with the value of this variable is stored in a dictionary, in the face of new variables will first query records in the dictionary, If you have the same record then it will re-use this value before this dictionary. So you example given, when the file executed (same block) will i1, i2 two variables point to the same object, then they meet the caching mechanism in the presence of only one memory, namely: the same id.

Suitable for :  int (float), str, BOOL.

:( understand the specific details of the object)

  int (float): any number in the same block of code are multiplexed.

  bool: True and False 1,0 way will exist in the dictionary, and reuse.

  str: Almost all of the strings will be in line with the caching mechanism, specific provisions are as follows ( understanding can! ):

1, a string obtained satisfy non multiplication code block cache mechanism:

s1 = 'Bai @! * ewq # ' 
s2 =' Bai @! ewq * # ' 
Print (IS S1 S2) # True

2, the string obtained multiplying two cases:

  2.1 is the multiplier 1, to meet any string code block cache mechanism:

b1 = 'Bai @ 5847395QQ0743895 * & ^% $ # ((& _ + (())' *. 1 
A1 = 'Bai @ 5847395QQ0743895 * & ^% $ # ((& _ + (())' *. 1 

Print (A1 IS b1) # True

  Multiplier 2.2> = 2: contains only uppercase and lowercase letters, numbers, underscores, the total length of <= 20, to meet code block cache mechanism:

s1 = 'old_' * 5
s2 = 'old_' * 5
print(s1 is s2)  # True

 Advantages : the ability to increase the number string, integer, character processing performance in time and space; requires the same value string, integer, when taken directly from a 'dictionary' are multiplexed, to avoid frequent creation and destruction, improve efficiency, save memory.

 

Small data pool

Small data pool, different code blocks of cache mechanism, also known as small integers caching mechanism, or the like mechanisms called dwell

Python automatically integer from -5 to 256 were cached , when you these integer assigned to the variable, and will not re-create the object, but the use of already created the cache object.

python string will be certain rules in the string resides pool , create a copy, when you these strings assigned to the variable, and will not re-create the object, but used in a string resides in the pool is created Object.

  In fact, both the cache or pool resides strings are made of a python optimization, is to string integer, and certain rules of -5 to 256, placed in a 'pool' (container, or dictionaries), whether the program variables that point to a string or integer within these ranges, then he quoted directly in the 'pool', the implication is to create a memory of.

Suitable for :  int (float), str, BOOL 

 

Guess you like

Origin www.cnblogs.com/chen55555/p/11284936.html