CPU 操作花费的时钟周期

OperationClock Cycles
Simple register-register op(ADD, OR)1
Memory write1
Bypass delay: switch between integer and floating-point units0 - 3
"Right" branch of "if"1 - 2
Floating-point/vector addition1 - 3
Multiplication(integer/float/vector)1 - 7
Return error and check1 - 7
L1 read3 - 4
TLB miss7 - 21
L2 read10 - 12
"Wrong" branch of "if"(branch misprediction)10 - 20
Floating-point division10 - 40
128-bit vector division10 - 70
Atomics/CAS15 - 30
C function direct call15 - 30
Integer division15 - 40
C function indirect call20 - 50
C++ virtual function call30 - 60
L3 read30 - 70
Main RAM read100 - 150
NUMA: different-socket atomics/CAS(guesstimate)100 - 300
NUMA: different-socket L3 read100 - 300
Allocation+Deallocation pair(small objects)200 - 500
NUMA: different-socket main RAM read300 - 500
Kernel call1000 - 1500
Thread context switch(direct costs)
C++ Exception thrown+caught
2000
5000 - 10000
Thread context switch(total costs, including cache invalidation)10000 - 1 million

Infographics: Operation Costs in CPU Clock Cycles - IT Hare on Soft.ware

Infographics: Operation Costs in CPU Clock Cycles

http://ithare.com/wp-content/uploads/part101_infographics_v08.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用C语言获取CPU时钟周期的流程如下: 1. 首先,需要确保编译器支持内联汇编语句。对于大多数常见的编译器,都支持内联汇编。 2. 然后,使用内联汇编语句来获取CPU时钟周期。不同的编译器和CPU架构有不同的语法,以下是一个示例: ``` #include <stdio.h> unsigned long long get_cycle_count() { unsigned int low, high; // 使用RDTSC(Read Time Stamp Counter)指令获取时钟周期计数 __asm__ volatile ("rdtsc" : "=a" (low), "=d" (high)); // 将低32位和高32位合并成一个64位的计数值并返回 return ((unsigned long long)high << 32) | low; } int main() { unsigned long long start, end, cycles; // 获取开始时的时钟周期数 start = get_cycle_count(); // 在这里执行需要计时的代码 // 获取结束时的时钟周期数 end = get_cycle_count(); // 计算经过的时钟周期cycles = end - start; printf("经过的时钟周期数:%llu\n", cycles); return 0; } ``` 上述代码中,`get_cycle_count()`函数使用了`rdtsc`指令来读取CPU的时间戳计数器(TSC)寄存器的值,该寄存器记录了从系统启动以来的时钟周期数。然后,通过将低32位和高32位合并成一个64位的计数值,函数返回了经过的时钟周期数。 在`main()`函数中,我们可以使用`get_cycle_count()`函数来获取开始和结束时的时钟周期数,并通过相减得到经过的时钟周期数。最后,使用`printf()`函数将结果打印出来。 需要注意的是,不同的编译器可能有不同的内联汇编语法,并且某些编译器可能对内联汇编的支持有所限制或要求特定的设置。因此,在使用此方法时,需要仔细查阅所用编译器的文档以了解正确的语法和用法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值