High precision timing

clock

#include <ctime>
#include <iostream>

int main() {
  	clock_t st = clock();
	// do something
    clock_t ed = clock();
    std::cout << ed - st << std::endl;
    std::cout << (ed - st) / CLOCKS_PER_SEC << std::endl;	
}

high_resolution_clock

Can achieve nanosecond accuracy:

#include <chrono>
#include <iostream>
int main() {
    typedef std::chrono::high_resolution_clock Clock;
    auto t1 = Clock::now();
    // do something
    auto t2 = Clock::now();
    std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(t2-t1).count() << '\n';
}

rdtsc/rdtscp

Access time for cache invalidation:

extern u_int64_t test(int *p, int i);
.section .text
.global test
.type test, @function             

test:
	movslq	%esi, %rsi
    rdtscp
    mov  %rax, %r10
    sal $32, %rdx
    or %rdx, %r10
	movslq	(%rdi,%rsi,4), %r11
    rdtscp
    sal $32, %rdx
    or %rdx, %rax
    sub %r10, %rax
    ret


Published 74 original articles · Like 11 · Visits 30,000+

Guess you like

Origin blog.csdn.net/yijiull/article/details/104803682