diff --git a/.drone.yml b/.drone.yml index b3b3d40..938afd0 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,24 +1,27 @@ -kind: pipeline -name: default -steps: - - name: test - image: gcc:latest - commands: - - g++ hello.cpp -o hello && ./hello - - - cd class || exit - - g++ construct.cpp -o construct && ./construct - - g++ inherit.cpp -o inherit && ./inherit - - cd - - - - cd stl || exit - - g++ map.cpp -o map && ./map - - cd - - - - cd interview || exit - - g++ memcache.cpp -o memcache && ./memcache - - cd - - - - cd misc || exit - - g++ sizeof.cpp -o sizeof && ./sizeof +kind: pipeline +name: default +steps: + - name: test + image: gcc:latest + commands: + - g++ hello.cpp -o hello && ./hello + + - cd class || exit + - g++ construct.cpp -o construct && ./construct + - g++ inherit.cpp -o inherit && ./inherit + - cd - + + - cd stl || exit + - g++ map.cpp -o map && ./map + - cd - + + - cd interview || exit + - g++ memcache.cpp -o memcache && ./memcache + - cd - + + - cd misc || exit + - g++ sizeof.cpp -o sizeof && ./sizeof + - g++ caching.cpp -o caching && ./caching + - echo "following program fails to show the effect of 'likely' due to CPU branch predictor." + - g++ likely.cpp -o likely && ./likely - cd - \ No newline at end of file diff --git a/misc/caching.cpp b/misc/caching.cpp new file mode 100644 index 0000000..9c34f96 --- /dev/null +++ b/misc/caching.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +using namespace std; + +const int SIZE = 256 * 1024 * 1024; +const int pagenum = 16 * 1024; +const int pagesize = SIZE / pagenum; + +void xorsum_seq(int *data) +{ + int xorsum = 0; + timeval start, end; + gettimeofday(&start, nullptr); + for (int page = 0; page < pagenum; page++) + { + for (int idx = 0; idx < pagesize; idx++) + xorsum ^= data[page * pagesize + idx]; + } + + gettimeofday(&end, nullptr); + double usec = (end.tv_sec - start.tv_sec) + (double)(end.tv_usec - start.tv_usec) / 1000000; + cout << __func__ << " duration: " << usec << ", xorsum: " << xorsum << endl; +} + +void xorsum_jmp(int *data) +{ + int xorsum = 0; + timeval start, end; + gettimeofday(&start, nullptr); + for (int idx = 0; idx < pagesize; idx++) + { + for (int page = 0; page < pagenum; page++) + xorsum ^= data[page * pagesize + idx]; + } + gettimeofday(&end, nullptr); + double usec = (end.tv_sec - start.tv_sec) + (double)(end.tv_usec - start.tv_usec) / 1000000; + cout << __func__ << " duration: " << usec << ", xorsum: " << xorsum << endl; +} + +int main() +{ + int *data = new int[SIZE]; + int xorsum = 0; + srand(time(nullptr)); + for (int idx = 0; idx < SIZE; idx++) + { + data[idx] = rand(); + } + xorsum_seq(data); + xorsum_jmp(data); +} \ No newline at end of file