Test Target
Test the time-consuming changes in the construction and sequencing of set, vector, and list in the case of increasing number of members, to find out the number of members that are time-consuming for set-time-consuming other containers
Test method
-
Set uses direct insertion
-
Vectors use assign constructs and uses global sort ordering
-
List uses the assign construct between the sort of members
-
The comparison is time-consuming, and does not care about the timing of the specific value, because different machine configuration is not the same
Test conclusion
Because of the number of consecutive times that are set, the number of member values is different, and the number of members that are now obtained is not accurate as a result of the increase in continuous exceeding the upper limit , such as:
The maximum number of members is about 700 in a row exceeding the upper limit of 10 o'clock
The maximum number of members is about 2000 in a row exceeding the upper limit of 20 o'clock
But one thing is certain: the set edge insertion edge sorting efficiency, no vector and list assignment or sorting is high, if there is a large number of data sorting situation, with the vector or list of the assignment after the performance of the comparison with the set is better.
Test code
Main logic main.cpp#include "TimeConsume.h" #include "Random.h" #include <unistd.h># include <vector> #include <list> #include <set> #include <algorithm># include <iostream> #include <cstdint> #include <string>using namespace Whether the std;// set time is continuously exceeding the other container Bool is_continue_beyond (uint64_t vector_time, uint64_t List_time, uint64_t set_time, uint64_t beyond_num) { static uint64_t s_beyond_num = beyond_num; if (set_time > list_time && set_time > vector_time) {--s_beyond_num; } else {s_beyond_num = beyond_num; } return s_beyond_num == 0;} Int main (INT&NBSP;ARGC,&NBSP;CHAR**&NBSP;ARGV) { uint64_t member_count_num = 0; uint64_t member_add_num = 0; uint64_t beyond_num = 0; if (argc != 4) {cout << "input: " << argv[0] << " Member_start_num member_add_num beyond_num " << endl;return -1; } else {member_count_num = strtoull (argv[1], null, 10); member_add_ Num = strtoull (argv[2], null, 10); Beyond_num = strtoull (Argv[3], NULL, &NBSP;10); } uint64_t vector_time = 0; uint64_t list_time = 0; uint64_t set_ time = 0; while (!is_continue_beyond (Vector_time, list_time, set_time, Beyond_num)) {vector<uint64_t > input_random_num; // Use the same random data random Random;random. Setrandomnum<vector<uint64_t> > (member_count_num, input_random_num);// structure sort function vector <uint64_t> monitor_vector; // externally define the container to prevent the time calculation resulting from the construction of the destructor auto vector_sort = [ &] () { monitor_vector.assign (Input_random_num.begin (), input_random_ Num.end ()); sort (Monitor_vector.begin (), monitor_vector.end ()); List<uint64_t> monitor_list;auto list_sort = [&] () { monitor_list.assign (Input_random_num.begin (), input_random_num.end ()); Monitor_list.sort ();}; Set<uint64_t> monitor_set;auto set_sort = [&] () { monitor_ Set.insert (Input_random_num.begin (), input_random_num.end ());};/ / Statistics Sort Time   Timeconsume vector_time_consume (vector_sort); timeconsume list_time_consume (list_sort); timeconsume set_time_ Consume (Set_sort); Vector_time = vector_time_consume. Getfntime (); List_time = list_time_consume. Getfntime (); Set_time = set_time_consume. Getfntime ();cout << "Count:" << member_count_num<< "\ T" < < "Vector_time:" << vector_time << "\ T" << "List_time: " << list_time << " \ T " << " Set_time: " << set_ Time << endl;sleep (0.5); member_count_num += member_add_num; } return 0;}
The/*timeconsume.h is used to get the time spent running the program, and the Support function object (c++11 new standard) takes a microsecond */#ifndef time_consume_h#define time_consume_h #include <sys/time.h> #include <ctime> #include <functional>using std:: function; #define SEC_RATIO_MSEC 1000#define SEC_RATIO_USEC (1000*SEC_RATIO_MSEC) class timeconsume {public: timeconsume (const function<void () > &monitor_fn = [] () {;}): m_monitor_fn (MONITOR_FN) { clear (); } ~timeconsume () {;} // set time-consuming monitoring of Function objects inline function<void () > SETMONITORFN (Const function<void () > &monitor_fn ()) { auto old_monitor_fn = m_monitor_fn; m_monitor_fn = monitor_fn; return old_monitor_fn; } // record the start of the monitoring point in time inline void Start () { gettimeofday (&m_start, null); } // record the end of the monitoring point in time inline void end ( ) { gettimeofday (&m_end, null); } // get microsecond time-consuming inline uint64_t getintervaltime () based on the point of start and end monitoring { if ((m_end.tv_sec > m_start.tv_sec) | | (m_end.tv_sec == m_start.tv_sec && m_end.tv_usec >= m_start.tv_ USEC) { return (m_end.tv_sec - m_start.tv_ SEC) *sec_ratio_usec + (m_end.tv_usec - m_start.tv_usec); } else { return 0; } } // the microsecond operation of acquiring a monitoring function object is time-consuming inline uint64_ T getfntime () { clear (); Start (); m_monitor_fn (); end (); return Getintervaltime (); }protected: // formatting internal correlation values inline void clear () { m_start.tv_sec = 0; m_start.tv_usec = 0; m_end.tv_sec = 0; m_end.tv_usec = 0; }private: struct timeval m_start; struct timeval m_end; Function<void () > m_monITOR_FN;}; #endif
/*random.h can fill the STL container with a specified number of random values, and take the value range at [0-rand_max],rand_max is the maximum value of the integer constant expression. This value depends on the implementation. Ensure that this value is at least 32767*/#ifndef random_h#define random_h#include <ctime> #include <iterator>using std::inserter; Class Random {public:random () {Srand (Time (NULL)); } ~random () {;} Template <typename containert> inline void Setrandomnum (uint64_t count, Containert &container) {Auto ITER = Inserter (Container, container.end ()); for (uint64_t i = 0; i < count; ++i) {iter = rand ();} }};
If you have any questions or suggestions about the test, you are welcome to discuss
Time-consuming testing of the construction and sequencing of set, vector, and list