Skip to content

Commit

Permalink
重做习题20-20,单独测量插入时间
Browse files Browse the repository at this point in the history
  • Loading branch information
ZZy979 committed Feb 18, 2024
1 parent e3078bc commit dbe4021
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,9 @@ $ ctest
* [20-18~20-19](ch20/range_checked_iterator.h)
* [20-20](ch20/exec20-20.cpp)
* 编译命令:`g++ -std=c++14 -O2 -o exec20-20 exec20-20.cpp`
* [测试结果](ch20/exec20-20_result.csv)(看起来似乎不存在`list``vector`快的可能性)
* [测试结果](ch20/exec20-20_result.csv)
* [总时间](ch20/exec20-20_total_time.png)
* [插入时间](ch20/exec20-20_insert_time.png)

## 第21章 算法和映射
### 21.4 函数对象
Expand Down
19 changes: 12 additions & 7 deletions ch20/exec20-20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,22 @@ using namespace std;
using namespace std::chrono;

template<class C>
system_clock::duration time_ordered_insert(C& c, int N) {
std::pair<system_clock::duration, system_clock::duration> time_ordered_insert(C& c, int N) {
random_device rd;
mt19937 generator(rd());
uniform_int_distribution<int> distribution(0, N - 1);

auto start = system_clock::now();
auto insert_time = system_clock::duration::zero();
for (int i = 0; i < N; ++i) {
int x = distribution(generator);
auto it = find_if(c.begin(), c.end(), [x](int y) { return y > x; });
auto before_insert = system_clock::now();
c.insert(it, x);
insert_time += system_clock::now() - before_insert;
}
auto stop = system_clock::now();
return stop - start;
auto total_time = system_clock::now() - start;
return std::make_pair(total_time, insert_time);
}

// Generate N random int values in the range [0:N), insert into a vector and keep it sorted.
Expand All @@ -37,11 +40,13 @@ int main(int argc, char* argv[]) {
using double_seconds = duration<double, seconds::period>;

vector<int> v;
system_clock::duration vector_time = time_ordered_insert(v, N);
cout << "vector takes " << duration_cast<double_seconds>(vector_time).count() << " s" << endl;
auto vector_time = time_ordered_insert(v, N);
cout << "vector total time: " << duration_cast<double_seconds>(vector_time.first).count() << " s, "
<< "insert time: " << duration_cast<double_seconds>(vector_time.second).count() << " s" << endl;

list<int> l;
system_clock::duration list_time = time_ordered_insert(l, N);
cout << "list takes " << duration_cast<double_seconds>(list_time).count() << " s" << endl;
auto list_time = time_ordered_insert(l, N);
cout << "list total time: " << duration_cast<double_seconds>(list_time.first).count() << " s, "
<< "insert time: " << duration_cast<double_seconds>(list_time.second).count() << " s" << endl;
return 0;
}
Binary file added ch20/exec20-20_insert_time.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 14 additions & 14 deletions ch20/exec20-20_result.csv
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
N,vector time (s),list time(s)
10000,0.0120641,0.0912781
20000,0.0481248,0.577694
50000,0.295525,6.22311
100000,1.213,30.2128
200000,5.65612,133.626
300000,13.096,319.202
400000,24.0716,699.424
500000,37.2407,1362.84
600000,54.6855,2491.17
700000,74.2577,4412.57
800000,96.2638,6896.5
900000,122.905,9786.17
1000000,150.045,13834
N,vector total time(s),list total time(s),vector insert time(s),list insert time(s)
10000,0.0126521,0.0960283,0.0034532,0.00157586
20000,0.0453952,0.590373,0.011398,0.00342591
50000,0.265029,6.32425,0.0621942,0.00988026
100000,1.08638,30.7264,0.274217,0.022183
200000,5.0392,134.288,1.59602,0.0481346
300000,11.6677,317.616,3.95115,0.0863665
400000,21.0138,682.499,7.28008,0.149247
500000,32.8607,1343.36,11.6863,0.222103
600000,47.6211,2501.56,16.9675,0.302561
700000,64.2485,4231.82,23.2764,0.481038
800000,84.0283,6650.76,30.5704,0.651316
900000,106.682,9842.69,39.2427,0.681576
1000000,131.703,13541.2,48.4753,0.878751
Binary file added ch20/exec20-20_total_time.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit dbe4021

Please sign in to comment.