鉴于此代码:
#include <iostream>
#include <chrono>
typedef unsigned long long T;
namespace Benchmark
{
T a, b;
inline void L_Add() { a += b; b += a; }
inline void Unroll_10() { L_Add(); L_Add(); L_Add(); L_Add(); L_Add(); L_Add(); L_Add(); L_Add(); L_Add(); L_Add(); }
inline void Unroll_100() { Unroll_10(); Unroll_10(); Unroll_10(); Unroll_10(); Unroll_10(); Unroll_10(); Unroll_10(); Unroll_10(); Unroll_10(); Unroll_10(); }
inline void Unroll_1000(T& a_, T& b_)
{
a = a_; b = b_;
Unroll_100(); Unroll_100(); Unroll_100(); Unroll_100(); Unroll_100(); Unroll_100(); Unroll_100(); Unroll_100(); Unroll_100(); Unroll_100();
a_ = a; b_ = b;
}
};
inline void L_Add(T& a, T& b) { a += b; b += a; }
inline void Unroll_10(T& a, T& b) { L_Add(a, b); L_Add(a, b); L_Add(a, b); L_Add(a, b); L_Add(a, b); L_Add(a, b); L_Add(a, b); L_Add(a, b); L_Add(a, b); L_Add(a, b); }
inline void Unroll_100(T& a, T& b) { Unroll_10(a, b); Unroll_10(a, b); Unroll_10(a, b); Unroll_10(a, b); Unroll_10(a, b); Unroll_10(a, b); Unroll_10(a, b); Unroll_10(a, b); Unroll_10(a, b); Unroll_10(a, b); }
inline void Unroll_1000(T& a, T& b) { Unroll_100(a, b); Unroll_100(a, b); Unroll_100(a, b); Unroll_100(a, b); Unroll_100(a, b); Unroll_100(a, b); Unroll_100(a, b); Unroll_100(a, b); Unroll_100(a, b); Unroll_100(a, b); }
int main()
{
std::chrono::high_resolution_clock::time_point StartTime, EndTime;
T a = 3;
T b = 5;
StartTime = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 1000000; ++i)
Benchmark::Unroll_1000(a, b);
EndTime = std::chrono::high_resolution_clock::now();
std::cout << a << " : " << std::chrono::duration_cast<std::chrono::milliseconds>(EndTime - StartTime).count() / 2000.0 << "ns" << std::endl;
a = 3;
b = 5;
StartTime = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 1000000; ++i)
Unroll_1000(a, b);
EndTime = std::chrono::high_resolution_clock::now();
std::cout << a << " : " << std::chrono::duration_cast<std::chrono::milliseconds>(EndTime - StartTime).count() / 2000.0 << "ns" << std::endl;
return 0;
}
这个输出:
12646046898197897264 : 0.3875ns
12646046898197897264 : 2.253ns
我想知道为什么“Benchmark::Unroll_1000(a, b)”的反汇编是:
add rax,rcx
add rcx,rax
而“Unroll_1000(a, b)”的反汇编是:
mov rax,qword ptr [rdx]
add qword ptr [rcx],rax
mov rax,qword ptr [rcx]
add qword ptr [rdx],rax
据我了解,他们应该产生相同的代码。有人可以启发我吗?在没有全局变量的情况下,如何实现与“Benchmark::Unroll_1000(a, b)”相同的性能?