Skip to content

Instantly share code, notes, and snippets.

@colesbury
Last active October 26, 2021 21:49
Show Gist options
  • Save colesbury/b865b0a7788db1e8e3d88c94dcc01fa6 to your computer and use it in GitHub Desktop.
Save colesbury/b865b0a7788db1e8e3d88c94dcc01fa6 to your computer and use it in GitHub Desktop.
extern void run();
int main(int argc, char* argv[]) {
run();
}
.PHONY: main
main: main.cpp libthread_local.so
g++ -std=c++11 -O2 main.cpp -L . -lthread_local -o main
libthread_local.so: thread_local.cpp
g++ -std=c++11 -O2 -fPIC -shared -o libthread_local.so thread_local.cpp
.PHONY: clear
clean:
rm -f main libthread_local.so
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <time.h>
#include <iostream>
#include <atomic>
double diff(timespec start, timespec end)
{
timespec temp;
if ((end.tv_nsec-start.tv_nsec)<0) {
temp.tv_sec = end.tv_sec-start.tv_sec-1;
temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
} else {
temp.tv_sec = end.tv_sec-start.tv_sec;
temp.tv_nsec = end.tv_nsec-start.tv_nsec;
}
return (double)temp.tv_sec + temp.tv_nsec * 1e-9;
}
template <typename Op>
double benchmark(Op op) {
const int N = 10000000;
timespec time1, time2;
op();
clock_gettime(CLOCK_MONOTONIC, &time1);
for (int i = 0; i < N; i++)
op();
clock_gettime(CLOCK_MONOTONIC, &time2);
double time = diff(time1, time2);
std::cout << (time/N)*1e9 << " ns \n";
return time;
}
__attribute__((noinline))
void add_thread_local() {
static thread_local int x;
x += 1;
}
__attribute__((noinline))
void add_global() {
static int x;
x += 1;
}
void run() {
benchmark([&]() {
add_thread_local();
});
benchmark([&]() {
add_global();
});
}
@colesbury
Copy link
Author

colesbury commented Oct 26, 2021

Results below. The difference between the two numbers approximates the "cost" of thread-locals access in shared libraries (about 1-1.5 ns).

Linux (Intel(R) Xeon(R) CPU E5-2698 v4 @ 2.20GHz):

3.61949 ns
1.9507 ns

macOS (2.4 GHz 8-Core Intel Core i9)

2.3994 ns
1.3577 ns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment