Today, I have nothing to do with it. I 've been reading about it for a long time on my computer.
When I saw 43rd entries, I found that, as mentioned above, the algorithm replaces the manual loop.
No doubt arises.
I remember the operator [] operation (subscript operation) was the most efficient loop when I read the vector source code ).
The algorithm is fast.
The following is a test code:
// Test class and call Function
Class AAA
{
Public:
Void MakeFull2 ()
{
}
};
// Create 1000000 pointers
Int nCount = 1000000;
Std: vector <AAA *> vAAA;
VAAA. resize (nCount );
For (size_t I = 0; I <nCount; ++ I)
VAAA [I] = new AAA;
// Time
DWORD start, end;
// Test member function call (STL algorithm)
Start = timeGetTime ();
Std: for_each (vAAA. begin (), vAAA. end (),
Std: mem_fun <void, AAA> (& AAA: MakeFull2 ));
End = timeGetTime ();
Std: cout <end-start <std: endl;
// Test member function call (STL iterator method)
Start = timeGetTime ();
Std: vector <AAA * >:: iterator itr_end = vAAA. end ();
For (std: vector <AAA * >:: iterator itr = vAAA. begin (); itr! = Itr_end; ++ itr)
(* Itr)-> MakeFull2 ();
End = timeGetTime ();
Std: cout <end-start <std: endl;
// Test the member function call (std: vector subscript access method)
Start = timeGetTime ();
Size_t count = vAAA. size ();
For (size_t I = 0; I <count; ++ I)
VAAA [I]-> MakeFull2 ();
End = timeGetTime ();
Std: cout <end-start <std: endl;
Test results: 237/208/103
The highest score is the subscript method.
The worst is the STL algorithm.
Why?
If we change the code of the iterator Method to the following (the method described in the book ):
// Test member function call (STL iterator method)
Start = timeGetTime ();
For (std: vector <AAA * >:: iterator itr = vAAA. begin (); itr! = VAAA. end (); ++ itr)
(* Itr)-> MakeFull2 ();
End = timeGetTime ();
Std: cout <end-start <std: endl;
Test results: 239/644/107
The result shows that the iterator method is the worst and the difference is very far.
Let's go back to the book and look at it. It turns out that the slow iterator operation is caused by the judgment part in the for loop.
Let's take a look at the std: vector: end () function.
Iterator end ()
{// Return iterator for end of mutable sequence
Return (iterator (_ Mylast, this ));
}
The problem here is very serious. It turns out that a new iterator was re-constructed when the end was accessed. See the problem.
The reason is in this sentence.
Therefore, we conduct optimization to construct a vector: end () only once, so we have the top test code.
In this way, the STL algorithm described in the book becomes the slowest.
In fact, the efficiency of std: for_each and std: vector: iterator loop methods is theoretically identical,
However, std: for_each has only one slow point:
After testing, I found that std: mem_fun <void, AAA> (& AAA: MakeFull2)
The time difference between the two lies in the constructor of the mem_fun class.
Now, I have explained the efficiency of std: for_each and std: vector: iterator loops. Why is the speed of subscript Access faster?
Check the source code first:
Const_reference operator [] (difference_type _ Off) const
{// Subscript
Return (* this + _ Off ));
}
This function only performs an addition operation. Compared to the other two types, it does not rely on various judgments and structures, so the speed is the fastest.
It can be seen that the speed of the vector is now.
However, not all capacities use operator [] for the fastest operation.
Because I only discussed vector here, and I have not explained other containers.
However, for ordered containers like vector, operator [] operations must be the fastest.
Just like list is non-ordered, the list won't work, and the list does not have the operator [] operation.
The end;