Skip to content

Instantly share code, notes, and snippets.

@lwjef
Last active November 22, 2023 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lwjef/b8e3e4395554fdcf1d9ea42a95b5ab7b to your computer and use it in GitHub Desktop.
Save lwjef/b8e3e4395554fdcf1d9ea42a95b5ab7b to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <chrono>
#include <cstdlib>
// 打印耗时情况.
inline void print_duration(std::chrono::steady_clock::time_point start_t,
std::chrono::steady_clock::time_point end_t)
{
std::cout << (std::chrono::duration<double>(end_t - start_t).count() / 60)
<< " minutes" << std::endl;
}
int main(int argc, char *argv[])
{
// 检查是否提供了足够的命令行参数
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <multiplier>" << std::endl;
return 1;
}
// 通过命令行参数获取数据规模的倍数
long long multiplier = std::atoll(argv[1]);
long long data_size = multiplier * 1000000; // 乘以100万
std::cout << "Data size: " << data_size << std::endl;
// 计时器start_t, end_t.
// 内存分配.
auto start_t = std::chrono::steady_clock::now();
std::vector<double> V(data_size);
auto end_t = std::chrono::steady_clock::now();
print_duration(start_t, end_t);
// 数据赋值.
start_t = std::chrono::steady_clock::now();
// 利用随机数生成器生成0.0到1.0之间的实数.
std::default_random_engine generator(time(NULL));
std::uniform_real_distribution<double> distribution(0.0, 1.0);
for (auto &x : V)
x = distribution(generator);
end_t = std::chrono::steady_clock::now();
print_duration(start_t, end_t);
// 输出排序前的前、中、后各20个数
std::cout << "Top 20 numbers before sorting:" << std::endl;
for (int i = 0; i < 20; ++i)
std::cout << V[i] << " ";
std::cout << std::endl;
std::cout << "Middle 20 numbers before sorting:" << std::endl;
int middle_start = V.size() / 2 - 10;
for (int i = middle_start; i < middle_start + 20; ++i)
std::cout << V[i] << " ";
std::cout << std::endl;
std::cout << "Bottom 20 numbers before sorting:" << std::endl;
for (int i = V.size() - 20; i < V.size(); ++i)
std::cout << V[i] << " ";
std::cout << std::endl;
// 串行排序.
start_t = std::chrono::steady_clock::now();
std::sort(V.begin(), V.end());
end_t = std::chrono::steady_clock::now();
print_duration(start_t, end_t);
// 输出排序后的前、中、后各20个数
std::cout << "Top 20 numbers after sorting:" << std::endl;
for (int i = 0; i < 20; ++i)
std::cout << V[i] << " ";
std::cout << std::endl;
std::cout << "Middle 20 numbers after sorting:" << std::endl;
for (int i = middle_start; i < middle_start + 20; ++i)
std::cout << V[i] << " ";
std::cout << std::endl;
std::cout << "Bottom 20 numbers after sorting:" << std::endl;
for (int i = V.size() - 20; i < V.size(); ++i)
std::cout << V[i] << " ";
std::cout << std::endl;
return 0;
}
#redmi_note12t
#for ((i=10; i<=1000; i+=10)); do echo -ne "Running with multiplier: $i\r"; duration=$(./1x_millionsort $i 2>&1 | tail -n 1 | grep -oP '\d+\.\d+'); echo -e "$i\t$duration" >> 1x_millionsort_samples.txt; done
10 0.00573235
20 0.0116948
30 0.017818
40 0.0240698
50 0.0302126
60 0.0365986
70 0.0434532
80 0.0496052
90 0.0561793
100 0.0622229
110 0.0683022
120 0.0755802
130 0.081641
140 0.0883825
150 0.0951266
160 0.101084
170 0.10774
180 0.114748
190 0.121166
200 0.127587
210 0.134567
220 0.141973
230 0.147694
240 0.154141
250 0.161356
260 0.168728
270 0.175522
280 0.18178
290 0.188506
300 0.195452
310 0.201363
320 0.207557
330 0.214289
340 0.221738
350 0.229719
360 0.235448
370 0.242063
380 0.249797
390 0.25569
400 0.261461
410 0.269929
420 0.276637
430 0.283837
440 0.290481
450 0.296357
460 0.303526
470 0.311297
480 0.316313
490 0.323585
500 0.33045
510 0.337375
520 0.344771
530 0.367044
540 0.35581
550 0.363209
560 0.372117
570 0.376444
580 0.38551
590 0.392873
600 0.399228
610 0.406722
620 0.412576
630 0.419624
640 0.425407
650 0.434759
660 0.440963
670 0.447512
680 0.454364
690 0.461765
700 0.468309
710 0.475026
720 0.482832
730 0.489414
740 0.493549
750 0.501952
760 0.510718
770 0.516873
780 0.523585
790 0.531355
800 0.535482
810 0.544321
820 0.550264
830 0.55682
840 0.564838
850 0.57152
860 0.579717
870 0.587803
880 0.59114
890 0.601928
900 0.607523
910 0.613902
920 0.619131
930 0.627338
940 0.634411
950 0.642111
960 0.649223
970 0.658157
980 0.663595
990 0.670922
1000 0.677422
#iphone11
#for i in $(seq 10 10 100); do duration=$(./1x_millionsort "$i" 2>&1 | tail -n 1 | grep -oE '[0-9]+\.[0-9]+'); echo ""$i" "$duration"" >> 1x_millionsort_samples.txt; done
10 0.04185
20 0.0870667
30 0.134317
40 0.190917
50 0.247233
60 0.320017
70 0.3873
80 0.4719
90 0.570133
100 0.640833
#redmi note12t 7+gen2 16+1t miui14.0.25 termux witout root
~/billionsort $ ./1x_millionsort_with_output 1000
Data size: 1000000000
0.0453841 minutes
0.146892 minutes
Top 20 numbers before sorting:
0.710428 0.713725 0.730323 0.709033 0.0674542 0.791879 0.119612 0.652799 0.71462 0.430757 0.961477 0.398502 0.918796 0.915671 0.0132333 0.159689 0.104977 0.980828 0.16606 0.362369
Middle 20 numbers before sorting:
0.595488 0.214935 0.105608 0.00225607 0.757109 0.969243 0.612656 0.685733 0.375628 0.274725 0.858273 0.37744 0.239181 0.160432 0.855331 0.511792 0.645375 0.125383 0.527212 0.982348
Bottom 20 numbers before sorting:
0.0720484 0.666592 0.793607 0.265451 0.0895571 0.234687 0.91173 0.756651 0.187581 0.601048 0.359737 0.184966 0.365278 0.828829 0.732769 0.300619 0.679023 0.159269 0.0137839 0.313238
0.674826 minutes
Top 20 numbers after sorting:
2.05979e-09 2.47174e-09 2.8837e-09 4.58524e-09 4.99719e-09 5.40915e-09 6.23307e-09 9.17047e-09 9.99439e-09 1.08183e-08 1.25198e-08 1.29318e-08 1.41677e-08 1.7929e-08 1.83409e-08 1.87529e-08 2.04544e-08 2.08664e-08 2.21023e-08 2.33381e-08
Middle 20 numbers after sorting:
0.500012 0.500012 0.500012 0.500012 0.500012 0.500012 0.500012 0.500012 0.500012 0.500012 0.500012 0.500012 0.500012 0.500012 0.500012 0.500012 0.500012 0.500012 0.500012 0.500012
Bottom 20 numbers after sorting:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
#iphone11 a13 4+128g ios17.1.1 a-shell
[Documents]$ ./1x_millionsort_with_output 100
Data size: 100000000
0.0027 minutes
0.02695 minutes
Top 20 numbers before sorting:
0.784576 0.456242 0.435064 0.24069 0.238883 0.1218
88 0.0906725 0.759472 0.571483 0.873311 0.719637 0
.105947 0.147232 0.998151 0.208634 0.944725 0.5982
83 0.326884 0.469001 0.185011
Middle 20 numbers before sorting:
0.536601 0.833201 0.47632 0.150435 0.198435 0.7122
85 0.462214 0.832592 0.20638 0.477513 0.440846 0.8
3016 0.717554 0.588809 0.593436 0.431448 0.0733869
0.669873 0.0628034 0.869404
Bottom 20 numbers before sorting:
0.025361 0.15281 0.425639 0.763142 0.866676 0.8745
06 0.146795 0.37132 0.0807489 0.74551 0.129115 0.0
528538 0.361505 0.305142 0.501889 0.920844 0.51069
8 0.669769 0.825265 0.955762
0.478667 minutes
Top 20 numbers after sorting:
1.95768e-08 2.75114e-08 2.96249e-08 3.33862e-08 4.29687e-08 5.09033e-08 9.05763e-08 9.39256e-08 9.59854e-08 9.63974e-08 1.23962e-07 1.3607e-07 1.37772e-07 1.38184e-07 1.4653e-07 1.5194e-07 1.54465e-07 1.54877e-07 1.55289e-07 1.55701e-07
Middle 20 numbers after sorting:
0.499902 0.499902 0.499902 0.499902 0.499902 0.499902 0.499902 0.499902 0.499902 0.499902 0.499902 0.499902 0.499902 0.499902 0.499902 0.499902 0.499902 0.499902 0.499902 0.499902
Bottom 20 numbers after sorting:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment