-rw-r--r-- 1300 sortbench-20240116/bench-stdsort.cc raw
#include <stdint.h> #include <stdlib.h> #include <stdio.h> #include <assert.h> #include <algorithm> long long ticks(void) { unsigned long long result; asm volatile(".byte 15;.byte 49;shlq $32,%%rdx;orq %%rdx,%%rax" : "=a"(result) :: "%rdx"); return result; } #define N 131072 #define TIMINGS 127 int32_t r[N] __attribute__((aligned(4096))); int32_t x[(TIMINGS+1)*N] __attribute__((aligned(4096))); int32_t y[N] __attribute__((aligned(4096))); long long t[TIMINGS+1] __attribute__((aligned(4096))); int main() { for (long long n = 1;n <= N;n += 1+(n/58)) { for (long long i = 0;i < n;++i) r[i] = random(); for (long long j = 0;j <= TIMINGS;++j) for (long long i = 0;i < n;++i) x[j*n+i] = r[i]; for (long long j = 0;j <= TIMINGS;++j) t[j] = ticks(); for (long long j = 0;j <= TIMINGS;++j) { t[j] = ticks(); std::sort(x+j*n,x+j*n+n); } for (long long i = 0;i < n;++i) y[i] = r[i]; std::sort(y,y+n); for (long long j = 0;j <= TIMINGS;++j) for (long long i = 0;i < n;++i) assert(y[i] == x[j*n+i]); for (long long i = 0;i < TIMINGS;++i) t[i] = t[i+1]-t[i]; std::sort(t,t+TIMINGS); printf("%lld %lld %lld %lld\n",n,t[TIMINGS/4],t[TIMINGS/2],t[(3*TIMINGS)/4]); } return 0; }