测试结果
测试平台
Apple 平台:
芯片: Apple M4
核总数: 10(4性能和6能效)
内存: 16 GBNOI Linux 2.0 平台:
架构: x86_64
CPU 运行模式: 32-bit, 64-bit
字节序: Little Endian
Address sizes: 39 bits physical, 48 bits virtual
CPU: 8
在线 CPU 列表: 0-7
每个核的线程数: 2
每个座的核数: 4
座: 1
NUMA 节点: 1
厂商 ID: GenuineIntel
CPU 系列: 6
型号: 142
型号名称: Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
步进: 10
CPU MHz: 621.044
CPU 最大 MHz: 3800.0000
CPU 最小 MHz: 400.0000
BogoMIPS: 4599.93
虚拟化: VT-x
L1d 缓存: 128 KiB
L1i 缓存: 128 KiB
L2 缓存: 1 MiB
L3 缓存: 6 MiB
NUMA 节点0 CPU: 0-7
Vulnerability Itlb multihit: KVM: Mitigation: Split huge pages
Vulnerability L1tf: Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable
Vulnerability Mds: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Meltdown: Mitigation; PTI
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; Full generic retpoline, IBPB conditional, IBRS_FW, STIBP conditional, RSB filling
Vulnerability Srbds: Mitigation; Microcode
Vulnerability Tsx async abort: Not affected
标记: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_ts
c art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm p
cid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti ssbd ibrs ibpb s
tibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec x
getbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp md_clear flush_l1d| 测试平台 | 编译器 | 编译参数 | Struct 大小(Byte) | const& 用时(秒) | 直接传值用时(秒) | |
|---|---|---|---|---|---|---|
| Apple | Apple clang version 17.0.0 (clang-1700.4.4.1) Target: arm64-apple-darwin24.6.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin | -std=c++17 -DUnit=short -DTot=134217728 -O0 | ||||
-std=c++17 -DUnit=short -DTot=134217728 -O1 | ||||||
-std=c++17 -DUnit=short -DTot=402653184 -O2 | ||||||
-std=c++17 -DUnit=short -DTot=402653184 -O3 -march=native | ||||||
| NOI Linux 2.0 | g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0 Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | -std=c++14 -static -DUnit=short -DTot=134217728 -O0 | ||||
-std=c++14 -static -DUnit=short -DTot=134217728 -O1 | ||||||
-std=c++14 -static -DUnit=short -DTot=402653184 -O2 | ||||||
-std=c++14 -static -DUnit=short -DTot=402653184 -O3 | ||||||
-std=c++14 -static -DUnit=short -DTot=402653184 -O3 -march=native | ||||||
测试代码
main.cpp:
#include <fstream>
#include <iostream>
//#define TC
#define CXX "clang++"
#define CXX_FLAGS "-std=c++17 -DUnit=short -DTot=134217728 -O0"
#define MACHINE_NAME "Apple"
int main() {
using namespace std;
system(CXX " --version > result.log");
system(CXX " ../rn_test.cpp -o rn_test " CXX_FLAGS);
system("./rn_test >> result.log");
string r1, tmp;
ifstream fin("result.log");
getline(fin, tmp);
r1 += tmp + ' ';
getline(fin, tmp);
r1 += tmp + ' ';
getline(fin, tmp);
r1 += tmp + ' ';
getline(fin, tmp);
r1 += tmp;
#ifndef TC
cout << "| " << MACHINE_NAME << " | ";
cout << "`" << r1 << "` | ";
#else
cout << "| ^ | ^ | ";
#endif
cout << "`" << CXX_FLAGS << "` | ";
size_t a, b, c;
double e, f;
fin >> b >> c >> e >> f;
cout << " $" << c << "$ | $" << b << "$ | $" << e << "$ | $" << f << "$ |\n";
for (int i = 0; i < 13; i++)
{
cout << "| ^ | ^ | ^ |";
fin >> b >> c >> e >> f;
cout << " $" << c << "$ | $" << b << "$ | $" << e << "$ | $" << f << "$ |\n";
}
system("rm rn_test result.log");
return 0;
}
../rn_test.cpp:
#include <algorithm>
#include <chrono>
#include <iostream>
#include <random>
template<size_t Sz, size_t Vp1, size_t Vp2>
struct TestI_R {
Unit inc[Sz];
};
template<size_t Sz, size_t Vp1, size_t Vp2>
bool operator<(const TestI_R<Sz, Vp1, Vp2>& x1, const TestI_R<Sz, Vp1, Vp2>& x2) {
return x1.inc[Vp1] * x1.inc[Vp2] + (x1.inc[Vp1] ^ x1.inc[Vp2]) <
x2.inc[Vp1] * x2.inc[Vp2] + (x2.inc[Vp1] ^ x2.inc[Vp2]);
}
template<size_t Sz, size_t Vp1, size_t Vp2>
struct TestII_V {
Unit inc[Sz];
};
template<size_t Sz, size_t Vp1, size_t Vp2>
bool operator<(TestII_V<Sz, Vp1, Vp2> x1, TestII_V<Sz, Vp1, Vp2> x2) {
return x1.inc[Vp1] * x1.inc[Vp2] + (x1.inc[Vp1] ^ x1.inc[Vp2]) <
x2.inc[Vp1] * x2.inc[Vp2] + (x2.inc[Vp1] ^ x2.inc[Vp2]);
}
template<size_t Sz, size_t Vp1, size_t Vp2>
void fillI(TestI_R<Sz, Vp1, Vp2>* arr, size_t n) {
static std::mt19937 rnd(1312537);
static std::uniform_int_distribution<Unit> dis(0, Sz - 1);
for (int i = 0; i < n; i++)
for (int j = 0; j < Sz; j++)
arr[i].inc[j] = dis(rnd);
}
template<size_t Sz, size_t Vp1, size_t Vp2>
void fillII(TestII_V<Sz, Vp1, Vp2>* arr, size_t n) {
static std::mt19937 rnd(1312537);
static std::uniform_int_distribution<Unit> dis(0, Sz - 1);
for (int i = 0; i < n; i++)
for (int j = 0; j < Sz; j++)
arr[i].inc[j] = dis(rnd);
}
template<size_t Sz, size_t N, size_t Vp1, size_t Vp2>
void test() {
printf("\n%lu %lu ", sizeof(Unit) * Sz, N);
auto arr1 = new TestI_R<Sz, Vp1, Vp2>[N];
auto arr2 = new TestII_V<Sz, Vp1, Vp2>[N];
fillI(arr1, N);
fillII(arr2, N);
std::pair<double, double> res;
auto start = std::chrono::high_resolution_clock::now();
std::sort(arr1, arr1 + N);
res.first = std::chrono::duration_cast<
std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start).count() / 1e3;
start = std::chrono::high_resolution_clock::now();
std::sort(arr1, arr1 + N);
res.second = std::chrono::duration_cast<
std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start).count() / 1e3;
printf("%.3f %.3f\n", res.first, res.second);
delete[] arr2;
delete[] arr1;
}
int main() {
using namespace std;
test<1, Tot, 0, 0>();
test<2, Tot / 2, 0, 1>();
test<3, Tot / 3, 0, 2>();
test<4, Tot / 4, 1, 3>();
test<5, Tot / 5, 2, 3>();
test<6, Tot / 6, 4, 5>();
test<7, Tot / 7, 0, 4>();
test<8, Tot / 8, 1, 6>();
test<10, Tot / 10, 4, 7>();
test<12, Tot / 12, 6, 11>();
test<14, Tot / 14, 3, 12>();
test<16, Tot / 16, 4, 13>();
test<24, Tot / 24, 5, 23>();
test<32, Tot / 32, 2, 19>();
return 0;
}