Hyper-Urgent evaluation of C + +over-eager Evaluationvs.Eager evaluationvs.Lazy Evaluation
The lazy evaluation of C + + is mentioned earlier: Don't pay any price for anything other than your program's function. Lazy evaluation can definitely improve the performance of your program when you always need to perform some sort of calculation, but the results are not always used. However, when the results of the calculation are always needed, our "rainy Day" can bring a great improvement to the performance of the program.
PS: As for eager evaluation is "take a step to see a step", if the current need for this result, then calculate the result. Whether or not it will be used later.
over-eager Evaluation
A few simple and common examples of the use of over-eager evaluation are good explanations:
The growth pattern of the vector of STL
In STL, vector space growth is the best example of eager evaluation, when the current space capacity full, when adding elements, we allocate memory twice times the current memory, and not just to load the current new element. The source code for the insert of the STL vector can be described as:
PS: The following code is truncated from: http://blog.csdn.net/shoulinjun/article/details/23450597
123456789101112131415161718192021222324252627282930313233343536373839 |
Template<class t>void Myvector<t>::insert (iteratorPosition, Size_type N,Constt& value) {if(n <= end_of_storage-finish) {/* enough memory */if(n <= finish-position) {std::uninitialized_copy (finish-n, Finish, finish); Std::copy (position, finish-n, position+n); Std::fill_n (position, n, value); }Else{Std::uninitialized_fill_n (Finish, N-(finish-position), value); Std::uninitialized_copy (position, finish, Position + N); Std::fill (position, finish, value); } finish + = n; }Else{/* reallocate */PointerNew_start (NULL), New_finish (NULL); Size_type old_type = End_of_storage-start; Size_type new_size = Old_type + Std::max (Old_type, N); New_start = Alloc.allocate (new_size); Copy old vector to new vector new_finish = std::uninitialized_copy (start, Position, New_start); Std::uninitialized_fill_n (New_finish, n, value); New_finish + = n; New_finish = std::uninitialized_copy (position, Finish, new_finish); Alloc.deallocate (start, End_of_storage-start); start = New_start; finish = New_finish; End_of_storage = New_start + new_size; } } |
Caching Technology
When reading a file on a disk, we can pre-read some of the overhead that is placed in memory, reducing the next read to that area, based on the "local principle" of the program execution.
Amortization is also to reduce the time complexity of a single operation. Min Max avg value for maintaining streaming data:
Maintain a data structure to hold the maximum or minimum or average value of the current existing, reducing the time complexity of a single operation (strictly speaking, the time complexity here refers to the average time complexity).
An example of changing space with time.
For example, it is required to maintain a stack that returns the minimum value, which is typically used to maintain the minimum value with an additional data structure, so that the time complexity of returning the minimum value is O (1). The code is:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
Class stack{Private:stack<int> elemstack;stack<int> Minstack;int Minelem;Public :Stack () {Minelem=int_max;} int getmin (); void Push (int val); void Pop ();}; int Stack::getmin () {if (Minstack.empty ()) {cout <<"Stack is empty ..."<< Endl;return Int_min;} Else{return minstack.top ();}} void Stack::P ush (int val) {if (Elemstack.empty ()) {Elemstack.push (val); Minstack.push (val);} Else{elemstack.push (val); if (Val < Minstack.top ()) {Minstack.push (val);} Else{minstack.push (Minstack.top ());}}} void Stack::P op () {if (Elemstack.empty ()) {cout <<"Stack is empty ..."<< Endl;return;} Elemstack.pop (); Minstack.pop ();} |
Final words:
- If you expect a program to often require an operation, you can "anticipate" and use data structures such as the cache above and min stack to reduce the single time complexity of the operation. So as to improve the operation efficiency of the whole program.
- Here the over-eager evaluation and lazy evaluation is not contradictory, the former is for certain will do the operation of a rainy day, the latter is not necessary for unnecessary operations to pay an additional price.
Over-eager Evaluation Advanced Evaluation of C + +