3 回答
TA贡献1848条经验 获得超2个赞
<chrono>
<chrono>
<chrono>
RDTSC
RDTSC
<chrono>
RDTSC
clock()
, clock_gettime()
QueryPerformanceCounter
.
RDTSC
QueryPerformanceCounter
clock_gettime()
std::chrono::high_resolution_clock
std::chrono::system_clock
rdtsc
x::clock
:
#include <chrono>namespace x{struct clock{ typedef unsigned long long rep; typedef std::ratio<1, 2'800'000'000> period; // My machine is 2.8 GHz typedef std::chrono::duration<rep, period> duration; typedef std::chrono::time_point<clock> time_point; static const bool is_steady = true; static time_point now() noexcept { unsigned lo, hi; asm volatile("rdtsc" : "=a" (lo), "=d" (hi)); return time_point(duration(static_cast<rep>(hi) << 32 | lo)); } }; } // x
now() {return __rdtsc();}
).
x::clock
#include <iostream>template <class clock>voidtest_empty_loop(){ // Define real time units typedef std::chrono::duration<unsigned long long, std::pico> picoseconds; // or: // typedef std::chrono::nanoseconds nanoseconds; // Define double-based unit of clock tick typedef std::chrono::duration<double, typename clock::period> Cycle; using std::chrono::duration_cast; const int N = 100000000; // Do it auto t0 = clock::now(); for (int j = 0; j < N; ++j) asm volatile(""); auto t1 = clock::now(); // Get the clock ticks per iteration auto ticks_per_iter = Cycle(t1-t0)/N; std::cout << ticks_per_iter.count() << " clock ticks per iteration\n"; // Convert to real time units std::cout << duration_cast<picoseconds>(ticks_per_iter).count() << "ps per iteration\n";}
std::chrono::nanoseconds
Cycle
clock::now()
typename clock::time_point t0 = clock::now();
x::clock
count()
std::chrono::duration_cast
Cycle
picoseconds
int main(){ std::cout << "\nUsing rdtsc:\n"; test_empty_loop<x::clock>(); std::cout << "\nUsing std::chrono::high_resolution_clock:\n"; test_empty_loop<std::chrono::high_resolution_clock>(); std::cout << "\nUsing std::chrono::system_clock:\n"; test_empty_loop<std::chrono::system_clock>();}
x::clock
std::chrono::high_resolution_clock
std::chrono::system_clock
Using rdtsc: 1.72632 clock ticks per iteration 616ps per iteration Using std::chrono::high_resolution_clock: 0.620105 clock ticks per iteration 620ps per iteration Using std::chrono::system_clock: 0.00062457 clock ticks per iteration 624ps per iteration
我的机器的时钟速度来定义 x::clock
.要测试的迭代次数。如果更改这个数目会使结果有很大差异,那么您可能应该提高迭代次数,或者在测试时清空计算机上的竞争进程。
TA贡献2016条经验 获得超9个赞
inline __int64 GetCpuClocks(){ // Counter struct { int32 low, high; } counter; // Use RDTSC instruction to get clocks count __asm push EAX __asm push EDX __asm __emit 0fh __asm __emit 031h // RDTSC __asm mov counter.low, EAX __asm mov counter.high, EDX __asm pop EDX __asm pop EAX // Return result return *(__int64 *)(&counter);}
- 3 回答
- 0 关注
- 740 浏览
添加回答
举报