跳到正文

个人记录

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

wfirstzhang约 28 分钟
原文
文章信息与版本

文章信息

发布
2025年11月30日
收录
2025年12月12日
更新
2026年11月7日
篇幅
约 28 分钟

版本

  1. v1当前

测试结果

测试平台

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
测试平台编译器编译参数NNStruct 大小(Byte)const& 用时(秒)直接传值用时(秒)
AppleApple 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 -O0134217728134217728220.6850.6850.6710.671
6710886467108864440.5710.5710.3510.351
4473924244739242660.8350.8350.3560.356
3355443233554432880.760.760.3110.311
268435452684354510100.7160.7160.2870.287
223696212236962112120.6350.6350.2460.246
191739611917396114140.6480.6480.2320.232
167772161677721616160.5830.5830.2130.213
134217721342177220200.5230.5230.190.19
111848101118481024240.4940.4940.1710.171
9586980958698028280.4510.4510.1560.156
8388608838860832320.4060.4060.1450.145
5592405559240548480.3830.3830.1120.112
4194304419430464640.3020.3020.0930.093
-std=c++17 -DUnit=short -DTot=134217728 -O1134217728134217728220.0740.0740.0680.068
6710886467108864440.1410.1410.0470.047
4473924244739242660.280.280.0470.047
3355443233554432880.2530.2530.040.04
268435452684354510100.2490.2490.0370.037
223696212236962112120.2250.2250.0330.033
191739611917396114140.2280.2280.0310.031
167772161677721616160.2010.2010.0290.029
134217721342177220200.1840.1840.0270.027
111848101118481024240.1810.1810.0270.027
9586980958698028280.1650.1650.0270.027
8388608838860832320.140.140.0280.028
5592405559240548480.1240.1240.0280.028
4194304419430464640.1030.1030.0290.029
-std=c++17 -DUnit=short -DTot=402653184 -O2402653184402653184220.3490.3490.2030.203
201326592201326592440.4270.4270.140.14
134217728134217728660.8440.8440.1410.141
100663296100663296880.7770.7770.1270.127
805306368053063610100.7420.7420.1110.111
671088646710886412120.6670.6670.10.1
575218835752188314140.6470.6470.0950.095
503316485033164816160.6170.6170.0880.088
402653184026531820200.5590.5590.0820.082
335544323355443224240.5190.5190.0850.085
287609412876094128280.4840.4840.0840.084
251658242516582432320.4620.4620.0880.088
167772161677721648480.3870.3870.0930.093
125829121258291264640.3260.3260.0970.097
-std=c++17 -DUnit=short -DTot=402653184 -O3 -march=native402653184402653184220.360.360.280.28
201326592201326592440.4250.4250.1430.143
134217728134217728660.8460.8460.1440.144
100663296100663296880.7760.7760.1310.131
805306368053063610100.7410.7410.1120.112
671088646710886412120.6680.6680.0990.099
575218835752188314140.6440.6440.0960.096
503316485033164816160.5910.5910.0880.088
402653184026531820200.5620.5620.0810.081
335544323355443224240.5270.5270.0860.086
287609412876094128280.4860.4860.0850.085
251658242516582432320.4620.4620.0870.087
167772161677721648480.3840.3840.0930.093
125829121258291264640.3230.3230.0960.096
NOI Linux 2.0g++ (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 -O01342177281342177282235.55635.55635.55935.559
67108864671088644421.12521.12520.67220.672
44739242447392426613.99313.99313.54913.549
33554432335544328810.4510.459.5429.542
268435452684354510108.4168.4167.6727.672
223696212236962112126.8516.8516.3316.331
191739611917396114146.0786.0785.5245.524
167772161677721616165.1595.1594.4984.498
134217721342177220204.1924.1923.6553.655
111848101118481024243.4553.4553.0393.039
9586980958698028282.9952.9952.5782.578
8388608838860832322.5912.5912.1442.144
5592405559240548481.8391.8391.5031.503
4194304419430464641.4391.4391.111.11
-std=c++14 -static -DUnit=short -DTot=134217728 -O1134217728134217728222.7872.7872.7872.787
6710886467108864443.8633.8633.6153.615
4473924244739242662.9042.9042.5282.528
3355443233554432882.2412.2411.8081.808
268435452684354510101.8821.8821.5241.524
223696212236962112121.5991.5991.2661.266
191739611917396114141.4461.4461.1271.127
167772161677721616161.2931.2930.9590.959
134217721342177220201.1131.1130.8290.829
111848101118481024241.0021.0020.7210.721
9586980958698028280.9130.9130.6720.672
8388608838860832320.8440.8440.5870.587
5592405559240548480.7840.7840.5580.558
4194304419430464640.7230.7230.5080.508
-std=c++14 -static -DUnit=short -DTot=402653184 -O2402653184402653184226.9786.9786.9916.991
201326592201326592447.9887.9887.1667.166
134217728134217728666.3596.3594.94.9
100663296100663296885.0785.0783.7533.753
805306368053063610104.6184.6183.393.39
671088646710886412123.7593.7592.662.66
575218835752188314143.8173.8172.7622.762
503316485033164816163.0323.0322.1912.191
402653184026531820202.6742.6741.9431.943
335544323355443224242.2982.2981.6411.641
287609412876094128282.3992.3991.7591.759
251658242516582432322.3632.3631.6941.694
167772161677721648482.1492.1491.631.63
125829121258291264642.1572.1571.5911.591
-std=c++14 -static -DUnit=short -DTot=402653184 -O3402653184402653184228.6718.6718.6688.668
201326592201326592447.5197.5196.7136.713
134217728134217728666.2526.2524.7064.706
100663296100663296884.6784.6783.5293.529
805306368053063610104.7184.7183.4493.449
671088646710886412123.6113.6112.6732.673
575218835752188314143.5623.5622.6162.616
503316485033164816163.3333.3332.5472.547
402653184026531820202.9092.9092.1952.195
335544323355443224243.1583.1582.5132.513
287609412876094128283.0183.0182.3232.323
251658242516582432323.3183.3182.7042.704
167772161677721648482.2572.2571.7111.711
125829121258291264642.2112.2111.6491.649
-std=c++14 -static -DUnit=short -DTot=402653184 -O3 -march=native402653184402653184226.8146.8147.017.01
201326592201326592447.4147.4146.6786.678
134217728134217728666.0126.0124.7294.729
100663296100663296885.3095.3094.0134.013
805306368053063610104.6534.6533.4593.459
671088646710886412123.6863.6862.6612.661
575218835752188314143.2173.2172.3982.398
503316485033164816163.3213.3212.4462.446
402653184026531820202.7642.7642.1372.137
335544323355443224242.4412.4411.7431.743
287609412876094128282.3292.3291.6891.689
251658242516582432323.3893.3892.8632.863
167772161677721648482.2172.2171.6731.673
125829121258291264642.3162.3161.8461.846

测试代码

main.cpp:

C++
#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:

C++
#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;
}
原文
存档评论2 条抓取于