Stackless Python First Experience

Source: Internet
Author: User

Stackless Python really destroys the third view, calculates the Fibonacci sequence, n is 100000 (100,000), and the run time is 2. About 2 seconds to write here. Sentiment: Stackless Python literally understands that there is no stack of Python, how to do not stack it? Stack-based language is how to implement: 1, the function is generally called the push stack inside, after the stack unit after the calculation, the first into the stack to complete 2, the unit inside the stack how to communicate? The experiment that just finished the DDoS attack today reminds me of this: the stack's unit communicates with its front and back units through the Ingress address and return address. 3, the thickness of the stack is limited, it seems to be more than 1000, that is, the iteration to 1000 layers can not continue into the stack, of course, can be considered to increase the number of layers. That's probably the way it works.  stackless Python says, I don't want stacks! So how do you write a program without a stack? 1, he used a thing called Tasklet to replace the stack of Unit 2, these units through a mechanism called Channel Communication 3, because not based on the stack, so the number of these tasklet you want how much, like here,100000  Tasklet is also called a micro-threading (Microthread), so it is a thread in the Python process that is then separated by "thread." It is designed to switch overhead between the various tasklet far less than the system's threads. A tasklet can send messages to another Tasklet channel, enter the blocking state himself, and then activate another tasklet  with the Fibonacci sequence as an example: As always, we use recursion to find the Fibonacci sequence .... Do not say, we all understand if you change the version of Stackless? The stack is gone, we have a tasklet, these tasklet contain a channel, a tasklet by passing their channel to the next Tasklet, The next channel implements communication by sending its own processing results to the previous Tasklet channel.   In fact, if you understand tasklet in a "micro-threading" way, you can think that it actually has a "stack", just like a system-level thread, but this stack is just for calling a function, or putting the call of this function into a tasklet.   In this way (in fact, I don't know if I understand it.) ), it forms a tasklet chain, and if we think of them as stacked shapes, it's quite similar to the shape of the stack, but they don't call stacks, called Tasklet, and performance is better than stack performance. Remember the name of this tool, it is calledDo stackless python.   Below is the code to calculate the Fibonacci sequence: Import stackless dic = {} def factorial (n): if n = = 1:return 1elif n = = 2:return 2else:retur N Task (n-1) + task (n-2)   def task (n): if STR (n) in Dic:return dic[str (n)]chann = Stackless.channel () Stackless.tasklet (COMPUTE) (N,chann) result = Chann.receive () dic[str (n)] = Resultreturn Result def compute (n, Chann): Return Chann.send (factorial (n))   print factorial (100000)   then try it with the stack version, The number of layers of the python stack is first changed to 100000, run, about 1.3 seconds to 1.5 seconds, in fact, stackless compared to the stack does not have much advantage.   Stack Version code: Import Syssys.setrecursionlimit (100000)  dic = {} def factorial (n): if n = = 1:return 1elif N = = 2:re Turn 2elif str (n) in Dic:return dic[str (n)]else:a = factorial (n-1) if STR (n-1) does in Dic:dic[str (n-1)] = AB = Factorial (n 2) if STR (n-2) not in Dic:dic[str (n-2)] = Areturn A + b  print factorial (99999)   Here is just a use of stackless, The general purpose of the stackless is to replace the system thread, to do concurrency with C language level of performance, this later to do the test

Stackless Python First experience

Related Article

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.