is a daffodil numberprintf ("%d", i);p rintf ("\ n") if the number is output and wraps.3. Problems encountered in commissioning process and PTA submission List status note
(1) At the time of compiling the problem, it was initially intended to use CNT to calculate the value of number.But in the title of the problem has been given the value of 153, the 4th step when the CNT was lost to 3The results have been partially correct.
With more than three digits, the number of daffodils cannot b
return result
Return wrapper
@memo
def fib (n):
If n return n
return fib (n-1) + fib (n-2)
In the example above, it is a recursive algorithm for a Fibonacci number example. We know that this recursion is quite inefficient because it will be called repeatedly. For example: We want to compute FIB (5
__repr__ functions are inherited by the quilt class.3. __cmp__ in PythonTo sort objects, You can use the function sorted function, provided the __cmp__ method of the class is Set.Class Student (object): def __init__ (self, name, score): self.name = name Self.score = Score def __str__ (self): return ' (%s:%s) '% (self.name, self.score) __repr__ = __str__ def __cmp__ (self, s): if Self.name >>> L = [Student (' Tim '), Student (' Bob ', Student),]>>> print s
result;7 }Here, given the size n, the number of executions of the basic step is 2n, so the algorithm complexity is O (2n).Example code (7):Fibonacci Sequence:
Fib (0) = 0
Fib (1) = 1
FIB (n) = fib (n-1) + fib (n-2)
F () = 0, 1, 1, 2, 3, 5, 8, 13, 2
hasattr (method, ' func_closure ') or method.func_closure is None:Raise Exception ("No closure for method.")method = Method.func_closure[0].cell_contentsReturn Get_true_argspec (method) Of course, I'm sure most people's programs don't go to Getargspec. Therefore, the use of functools wraps should be enough.
Some examples of decoratorOK, now let's take a look at the various decorator examples:
caching function calls This example is really too classic, the whole online Use this example to do de
fast-switching cache content is changing, and the burden of rebuilding cache is increasing, This results in lower router performance. CEF Express Exchange Technology is aimed at the above deficiencies and design proposed.
The basic principle of CEF Express Exchange
1.CEF Parts
CEF is an advanced third-tier switching technology, designed primarily for high-performance, highly scalable, third-tier IP backbone Exchange. To optimize packet forwarding routing lookup mechanisms, CEF de
, and most of them can be converted to each other.
Recursive call functions not only waste space, but can also easily overflow the stack if recursion is too deep.
Four, number-form recursion
As described earlier, tree recursion increases exponentially with the amount of information that is entered. The typical Fibonacci sequence is:
The sum of the first two digits in the Fibonacci sequence is equal to the third digit: 0,1,1,2,3,5,8,13,21 ...
The recursive implementation code is as follo
of the first two numbers (0, 1, 2, 3, 5, 8, 13, 21 ,...), A good introduction to the number of Fibonacci and related mathematical concepts is provided at www. MCS. surrey. AC. UK/personal/R. knott/Fibonacci/fib.html: Select 42, not because I am a fan of Douglas Adams, but because the 32 with the symbol of 47 is an integer, therefore, a smaller number is selected.The computation of the Fibonacci series is a classic example of recursive functions, because the number of Fibonacci is the sum of the
This article mainly introduces how to use the Psyco module in Python to optimize the running speed. the Psyco module allows your Python program to run as fast as the C language. This article provides multiple code examples, I also explained how to install and use Psyco. if you need it, you can refer to the introduction of the Psyco module today. the Psyco module allows your Python program to run as fast as the C language.
Python is easy to learn, but its performance is much inferior to that of s
Python uses the Psyco module to optimize the running speed.
Today we will introduce the Psyco module, which enables your Python program to run as fast as the C language.Python is easy to learn, but its performance is much inferior to that of some compilation languages (such as C). Here, you can use C and Python to compile the Fibonacci series computing program, and calculate the running time:
C Language ProgramCopy codeThe Code is as follows:Int fib (
mistakes.If the calculated algorithm is more complex, with a type-like list generated for the loop can not be realized, but also with the function, for example, the famous Fibonacci columns (Fibonacci), in addition to the first and second numbers, any number can be added by the preceding two numbers:1, 1, 2, 3, 5, 8, 13, 21, 34, ....The Fibonacci number column (Fibonacci) is not written in list generation, but it is easy to print it out with a function#!/usr/bin/env python3# -*- coding:utf-8 -*
= "") Use ($open, $close) {
Return "$open $inner$close";
};
}
If you are using the usual method, we will put inner into the HTML function parameters, so that no matter whether the code read or use the closure.
3. Release recursive function
Copy Code code as follows:
$fib = function ($n) use ( $fib) {
if ($n = = 0 | | $n = = 1) return 1;
Return $
them less practical and is not the first choice when profiling a program.However, when other performance analysis methods are not sufficient or inaccurate, they can be used as a final choice.Back to the top Python simple sample code for event-based performance analyzerImport sys def profiler (frame, event, arg): print ' Profiler:%r%r '% (event, Arg) Sys.setprofile (Profiler) #simpl E (and very ineficient) example of how to calculate the Fibonacci sequence for a number.def
. This method returns an iteration object. Then, the for loop of Python constantly calls the next () method of the iteration object to get the next value of the loop until the loop is exited when the StopIteration error occurs.
Let's take the Fibonacci series as an example. Writing a Fib class can act on the for Loop:
The Code is as follows:
Class Fib (object ):Def _ init _ (self ):Self. a, self. B = 0, 1 #
Today, the Psyco module, the Psyco module, enables your Python program to run as fast as C.Python is said to be easy to use, but performance is much worse than some compiled languages (such as C), where you can write Fibonacci sequence calculations in C and Python languages, and calculate the elapsed time:
C Language Program
Copy Code code as follows:
int fib (int n) {
if (n return n;
Else
return
This section describes the route tables of all routers, including the route table and FIB table. The route table is used to determine the route, and the FIB is used to forward the group, the route table will activate the route and send it to the FIB table. When the packet arrives at the router, it will forward it by searching the
self. step
Def _ iter _ (self ):
"Returns the iterator itself ."""
Return self
For el in MyIterator (4 ):
Print el
--------------------
Result:
3
2
1
0
Ii. Generators
Since Python2.2, the generator provides a simple way to return functions of list elements to complete simple and effective code.
It allows you to stop a function and return results immediately based on the yield command.
This function saves the execution context. If necessary, you can continue execution immediately.
For example, t
array object) [1]. (A common feature in C # language design is to insist on using a single clear form to differentiate different functions .)
A quick overview: Garbage Collection)As shown in the following array object, when we allocate space for the reference type in the managed heap (managed heap:
Int [] fib = new int [6 };
The number of handles that the object automatically maintains. In this example, an associated Reference Counter of the arr
, indicating that the process itself is referenced in the definition of this process (either directly or indirectly ."
Recursive calculation process:"When a computing process has a certain pattern (for example, linear recursion), we are talking about the progress of this computing process, rather than the syntax format written in the corresponding process ."
We generally like to use the Fibonacci series as an example when discussing recursion. The Fibonacci algorithm is also very simple. the a
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.