diff options
| author | KunoiSayami <[email protected]> | 2023-05-10 00:57:26 +0800 |
|---|---|---|
| committer | KunoiSayami <[email protected]> | 2023-05-10 00:57:26 +0800 |
| commit | c748e1ed7c9693931ae9ee61cf7c45aee2305728 (patch) | |
| tree | d015f34d03de8621e60c1080d7dbcc0bbfad92b7 /normal_distribution.cpp | |
| parent | 4ecb5d161c8eecafda5af12becaae8dbfba47c17 (diff) | |
feat: Use thread to show progress
Signed-off-by: KunoiSayami <[email protected]>
Diffstat (limited to 'normal_distribution.cpp')
| -rw-r--r-- | normal_distribution.cpp | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/normal_distribution.cpp b/normal_distribution.cpp index 93aff27..20ef61c 100644 --- a/normal_distribution.cpp +++ b/normal_distribution.cpp @@ -1,8 +1,10 @@ #include <algorithm> #include <cassert> +#include <chrono> #include <cstdio> #include <random> #include <set> +#include <thread> #include <vector> inline void @@ -37,6 +39,18 @@ void checkFileValid(const std::string &name, size_t length) { } } +void progress_bar(const std::set<long long> &v, size_t length) { + size_t previous_rate = 0; + while (v.size() < length) { + fprintf(stderr, "\rProgress %.02f%%(%lu) rate: %lu/s", + (double)v.size() * 100 / (double)length, v.size(), + v.size() - previous_rate); + previous_rate = v.size(); + std::this_thread::sleep_for(std::chrono::seconds(1)); + } + fputs("", stderr); +} + int main(int argc, char const *argv[]) { if (argc == 3) { @@ -67,17 +81,17 @@ int main(int argc, char const *argv[]) { vector.reserve(length); + std::thread progress_tl(progress_bar, std::ref(set), length); + while (set.size() != length) { auto element = (long long)std::round(normalDistribution(randomEngine)); auto ret = set.insert(element); if (ret.second) { vector.push_back(element); - - fprintf(stderr, "\rProgress %.02f%% %lu", - (double)set.size() * 100 / (double)length, set.size()); } } - fputs("", stderr); + + progress_tl.join(); assert(vector.size() == length); |
