关于比较传值和传引用的不严谨测试

· · 个人记录

测试结果

:::info[测试平台]{open} Apple 平台:

  芯片:   Apple M4
  核总数:  10(4性能和6能效)
  内存:   16 GB

NOI 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

:::

::cute-table{tuack}

测试平台 编译器 编译参数 N 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 134217728 2 0.685 0.671
^ ^ ^ 67108864 4 0.571 0.351
^ ^ ^ 44739242 6 0.835 0.356
^ ^ ^ 33554432 8 0.76 0.311
^ ^ ^ 26843545 10 0.716 0.287
^ ^ ^ 22369621 12 0.635 0.246
^ ^ ^ 19173961 14 0.648 0.232
^ ^ ^ 16777216 16 0.583 0.213
^ ^ ^ 13421772 20 0.523 0.19
^ ^ ^ 11184810 24 0.494 0.171
^ ^ ^ 9586980 28 0.451 0.156
^ ^ ^ 8388608 32 0.406 0.145
^ ^ ^ 5592405 48 0.383 0.112
^ ^ ^ 4194304 64 0.302 0.093
^ ^ -std=c++17 -DUnit=short -DTot=134217728 -O1 134217728 2 0.074 0.068
^ ^ ^ 67108864 4 0.141 0.047
^ ^ ^ 44739242 6 0.28 0.047
^ ^ ^ 33554432 8 0.253 0.04
^ ^ ^ 26843545 10 0.249 0.037
^ ^ ^ 22369621 12 0.225 0.033
^ ^ ^ 19173961 14 0.228 0.031
^ ^ ^ 16777216 16 0.201 0.029
^ ^ ^ 13421772 20 0.184 0.027
^ ^ ^ 11184810 24 0.181 0.027
^ ^ ^ 9586980 28 0.165 0.027
^ ^ ^ 8388608 32 0.14 0.028
^ ^ ^ 5592405 48 0.124 0.028
^ ^ ^ 4194304 64 0.103 0.029
^ ^ -std=c++17 -DUnit=short -DTot=402653184 -O2 402653184 2 0.349 0.203
^ ^ ^ 201326592 4 0.427 0.14
^ ^ ^ 134217728 6 0.844 0.141
^ ^ ^ 100663296 8 0.777 0.127
^ ^ ^ 80530636 10 0.742 0.111
^ ^ ^ 67108864 12 0.667 0.1
^ ^ ^ 57521883 14 0.647 0.095
^ ^ ^ 50331648 16 0.617 0.088
^ ^ ^ 40265318 20 0.559 0.082
^ ^ ^ 33554432 24 0.519 0.085
^ ^ ^ 28760941 28 0.484 0.084
^ ^ ^ 25165824 32 0.462 0.088
^ ^ ^ 16777216 48 0.387 0.093
^ ^ ^ 12582912 64 0.326 0.097
^ ^ -std=c++17 -DUnit=short -DTot=402653184 -O3 -march=native 402653184 2 0.36 0.28
^ ^ ^ 201326592 4 0.425 0.143
^ ^ ^ 134217728 6 0.846 0.144
^ ^ ^ 100663296 8 0.776 0.131
^ ^ ^ 80530636 10 0.741 0.112
^ ^ ^ 67108864 12 0.668 0.099
^ ^ ^ 57521883 14 0.644 0.096
^ ^ ^ 50331648 16 0.591 0.088
^ ^ ^ 40265318 20 0.562 0.081
^ ^ ^ 33554432 24 0.527 0.086
^ ^ ^ 28760941 28 0.486 0.085
^ ^ ^ 25165824 32 0.462 0.087
^ ^ ^ 16777216 48 0.384 0.093
^ ^ ^ 12582912 64 0.323 0.096
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 134217728 2 35.556 35.559
^ ^ ^ 67108864 4 21.125 20.672
^ ^ ^ 44739242 6 13.993 13.549
^ ^ ^ 33554432 8 10.45 9.542
^ ^ ^ 26843545 10 8.416 7.672
^ ^ ^ 22369621 12 6.851 6.331
^ ^ ^ 19173961 14 6.078 5.524
^ ^ ^ 16777216 16 5.159 4.498
^ ^ ^ 13421772 20 4.192 3.655
^ ^ ^ 11184810 24 3.455 3.039
^ ^ ^ 9586980 28 2.995 2.578
^ ^ ^ 8388608 32 2.591 2.144
^ ^ ^ 5592405 48 1.839 1.503
^ ^ ^ 4194304 64 1.439 1.11
^ ^ -std=c++14 -static -DUnit=short -DTot=134217728 -O1 134217728 2 2.787 2.787
^ ^ ^ 67108864 4 3.863 3.615
^ ^ ^ 44739242 6 2.904 2.528
^ ^ ^ 33554432 8 2.241 1.808
^ ^ ^ 26843545 10 1.882 1.524
^ ^ ^ 22369621 12 1.599 1.266
^ ^ ^ 19173961 14 1.446 1.127
^ ^ ^ 16777216 16 1.293 0.959
^ ^ ^ 13421772 20 1.113 0.829
^ ^ ^ 11184810 24 1.002 0.721
^ ^ ^ 9586980 28 0.913 0.672
^ ^ ^ 8388608 32 0.844 0.587
^ ^ ^ 5592405 48 0.784 0.558
^ ^ ^ 4194304 64 0.723 0.508
^ ^ -std=c++14 -static -DUnit=short -DTot=402653184 -O2 402653184 2 6.978 6.991
^ ^ ^ 201326592 4 7.988 7.166
^ ^ ^ 134217728 6 6.359 4.9
^ ^ ^ 100663296 8 5.078 3.753
^ ^ ^ 80530636 10 4.618 3.39
^ ^ ^ 67108864 12 3.759 2.66
^ ^ ^ 57521883 14 3.817 2.762
^ ^ ^ 50331648 16 3.032 2.191
^ ^ ^ 40265318 20 2.674 1.943
^ ^ ^ 33554432 24 2.298 1.641
^ ^ ^ 28760941 28 2.399 1.759
^ ^ ^ 25165824 32 2.363 1.694
^ ^ ^ 16777216 48 2.149 1.63
^ ^ ^ 12582912 64 2.157 1.591
^ ^ -std=c++14 -static -DUnit=short -DTot=402653184 -O3 402653184 2 8.671 8.668
^ ^ ^ 201326592 4 7.519 6.713
^ ^ ^ 134217728 6 6.252 4.706
^ ^ ^ 100663296 8 4.678 3.529
^ ^ ^ 80530636 10 4.718 3.449
^ ^ ^ 67108864 12 3.611 2.673
^ ^ ^ 57521883 14 3.562 2.616
^ ^ ^ 50331648 16 3.333 2.547
^ ^ ^ 40265318 20 2.909 2.195
^ ^ ^ 33554432 24 3.158 2.513
^ ^ ^ 28760941 28 3.018 2.323
^ ^ ^ 25165824 32 3.318 2.704
^ ^ ^ 16777216 48 2.257 1.711
^ ^ ^ 12582912 64 2.211 1.649
^ ^ -std=c++14 -static -DUnit=short -DTot=402653184 -O3 -march=native 402653184 2 6.814 7.01
^ ^ ^ 201326592 4 7.414 6.678
^ ^ ^ 134217728 6 6.012 4.729
^ ^ ^ 100663296 8 5.309 4.013
^ ^ ^ 80530636 10 4.653 3.459
^ ^ ^ 67108864 12 3.686 2.661
^ ^ ^ 57521883 14 3.217 2.398
^ ^ ^ 50331648 16 3.321 2.446
^ ^ ^ 40265318 20 2.764 2.137
^ ^ ^ 33554432 24 2.441 1.743
^ ^ ^ 28760941 28 2.329 1.689
^ ^ ^ 25165824 32 3.389 2.863
^ ^ ^ 16777216 48 2.217 1.673
^ ^ ^ 12582912 64 2.316 1.846

测试代码

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;
}