How is this so fast? <C>
The test for the builtin strlen function runs so fast because the function call gets optimized away. The compiler knows you aren't doing anything useful with strlen's return value, so it's free to do whatever it likes in this case.
This code in the C source file:
clock_gettime(CLOCK_MONOTONIC, &start); for (int i = 0; i < iteration; i++) strlen(str); clock_gettime(CLOCK_MONOTONIC, &end);
corresponds to this code in the assembly output, the first loop after main begins:
call clock_gettime movl $0, -4(%rbp) jmp .L24 .L25: incl -4(%rbp) .L24: cmpl $99, -4(%rbp) jle .L25 leaq -64(%rbp), %rax movq %rax, %rdx movl $1, %ecx call clock_gettime
The for loop corresponds to the instruction after .L25 and the two after .L24. As you can see, there's no call to strlen in there. To fix this, you need to find a way to prevent the compiler from optimizing away any function calls. One way to do this, as we've seen in previous posts, is to take a pointer to the function you want to test and pass it to another function that runs the benchmark. Here's an example I adapted from your code above as well as @Eigenvektor's previous benchmarking code.
#include <stdio.h> #include <time.h> #include <string.h> #define iteration 1000000 char* str = "Put long string here"; void benchmark(size_t (*functionPointer)(const char*)) { struct timespec start, end; clock_gettime(CLOCK_MONOTONIC, &start); for (int i = 0; i < iteration; i++) { functionPointer(str); } clock_gettime(CLOCK_MONOTONIC, &end); printf("elapsed: %ld\n", end.tv_nsec - start.tv_nsec); } int main() { benchmark(&strlen); return 0; }
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now