Can you think on any good reason why atomic operations seems slower than semaphores, even though there is a decrease on instructions?
Sample code:
void increment(){
if (strcmp(type, "ATOMIC") == 0) {
for (int i = 0; i < RUN_TIME; ++i) {
atomic_fetch_add_explicit(&count, 1, memory_order_relaxed);
}
}
if (strcmp(type, "SEMAPHORE") == 0){
for (int i = 0; i < RUN_TIME; ++i) {
sem_wait(sem);
count++;
sem_post(sem);
}
}
}
Output:
time ./CMAIN "SEMAPHORE";time ./CMAIN "ATOMIC";
[C] SEMAPHORE, count 4000000
real 0m0.039s
user 0m0.029s
sys 0m0.002s
[C] ATOMIC, count 4000000
real 0m0.092s
user 0m0.236s
sys 0m0.003s