0

I have the following assembly program, and as a sort of test, I'd like to see how the 1B-increment function takes. What would be the most accurate way to time this?

SYS_EXIT = 60
LOOP_MAX = 1000000000
.globl _start

loop:
    # do a billion increments
    mov $0, %eax
  _loop:
    inc %eax
    cmp $LOOP_MAX, %eax
    jne _loop
    ret

_start:

    # start time
    call loop
    # printf("It took %.8f to run", end_time - start_time)

    mov $0,  %edi
    mov $SYS_EXIT, %eax
    syscall
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
David542
  • 104,438
  • 178
  • 489
  • 842
  • 1
    You can use the [rdtsc](https://www.felixcloutier.com/x86/rdtsc) instruction but it's not so simple to convert it to a real time unit like seconds. Or you can invoke a system call like `clock_gettime`. – Nate Eldredge Feb 19 '21 at 03:58
  • @NateEldredge I see -- would you want to show an example of how to do that? I suppose getting the number of click cycles difference is fine for this purpose. – David542 Feb 19 '21 at 04:03
  • There's not a lot to say: it gives you a count of clock cycles in `edx:eax`. If you want it in a 64-bit register, you can do `mov %edx, %ecx ; shl $32, %rcx ; add %rax, %rcx`. Then do it again after your loop completes, and subtract. – Nate Eldredge Feb 19 '21 at 04:10
  • 1
    @NateEldredge: If you're timing stuff like this in asm, it usually makes the most sense to time in terms of core clock cycles (to verify that this loop runs at exactly the expected 1 iteration per cycle). In that case, [RDTSCP in NASM always returns the same value (timing a single instruction)](https://stackoverflow.com/q/54621381) shows a complete example of timing a loop in a static executable using `perf stat`. – Peter Cordes Feb 19 '21 at 04:24
  • @PeterCordes perfect, thanks for that link. By the way: what do you mean by `*core* clock cycles` ? – David542 Feb 19 '21 at 04:30
  • 1
    I mean ticks of the actual clock that the CPU core runs on. If your CPU is at its max turbo of 4.2GHz for example, the core clock is ticking at 4.2GHz. As opposed to reference frequency used by the TSC (which RDTSC reads); it ticks at a constant frequency regardless of what the CPU core is doing, making it a useful time source for absolute real-world time, but not processor cycles. (e.g. on my 4GHz i7-6700k, at 4008MHz). See [How to get the CPU cycle count in x86\_64 from C++?](https://stackoverflow.com/a/51907627) for more about RDTSC. – Peter Cordes Feb 19 '21 at 04:38

0 Answers0