Intel base instruction -- rdtsc

# CPU实时运行周期计数器

unsigned long long get_clock()
{
    unsigned long long ret;

    /* RDTSC : Read time stamp counter. */
    __asm__ __volatile__ ("rdtsc\n\t":"=A"(ret):);

    return ret;
}

int add(int a, int b)
{
    return a + b;
}

int main()
{
    unsigned long long cycle_new = 0, cycle_old = 0;
    int sum = 0;

    cycle_old = get_clock();
    sum = add(1000000, 1000000);
    cycle_new = get_clock();
    printf("cycle = %lld\n", cycle_new - cycle_old);
}

可以用来查看函数运行需要多少个CPU时钟周期。

你可能感兴趣的:(Assembly,linux)