Code study: division

disassemble,gdb中可以用disassemble命令反汇编。
image.png
image.pngimage.png
image.pngimage.png
image.png

Write Assembly Code: timer.s

  1. # File: timer.s
  2. # ---------------
  3. # Returns a 64-bit value for the number of milliseconds since
  4. # the computer was restarted
  5. .section .text
  6. .type milliseconds, @function
  7. .globl milliseconds
  8. THOUSAND:
  9. .int 1000 # 32-bit int
  10. milliseconds:
  11. # your code here
  12. rdtsc
  13. # Move edx to the upper 32 bits.
  14. shlq $32, %rdx
  15. # Ensure the upper 32 bits of rax are 0s.
  16. shlq $32, %rax
  17. shrq $32, %rax
  18. # Effectively concatenate
  19. addq %rdx, %rax
  20. # Make way for mulq, divq
  21. movq $0x0, %rdx
  22. # arithmetic
  23. movq $1000, %rsi
  24. mulq %rsi
  25. divq %rdi
  26. ret

Code study: loops

image.pngimage.png
image.pngimage.png
image.pngimage.png

loop unrolling

image.png
image.pngimage.png
image.png
image.png
A运行时间是版本B、C版本的两倍,为什么???