2014-11-29

izard: (Default)
2014-11-29 11:09 am
Entry tags:

[memory location]++ that takes ~10us on x86

Here is teh code
static unsigned char array[128];
void main()
{
    for (i = 0; i < 64; i++) if ((int)(array + i) % 64 == 63) break;
    lock = (unsigned int*)(array + i);
    for (i = 0; i < 1024; i++) *(lock)++; // prime
    // TSC1
    for (i = 0; i < 1024*8; i++)
    {
       asm volatile (
        "lock xaddl %1, (%0)\n"
        : // no output
        : "r" (lock), "r" (1));
    }
    // TSC2
    printf(" %i\n", (TSC2-TSC1)/1024*8);
}

Normal memory increment never takes more than couple of hundreds nanoseconds. Unless it is a locked cache line split increment. Good catch, obvious reasons.