diff --git a/Presentation1.pptx b/Presentation1.pptx deleted file mode 100644 index da46ac1..0000000 Binary files a/Presentation1.pptx and /dev/null differ diff --git a/specifelse/1.cpp b/specifelse/1.cpp new file mode 100644 index 0000000..fdafbb6 --- /dev/null +++ b/specifelse/1.cpp @@ -0,0 +1,7 @@ +int func(int x) { + if (x > 0) { + return 1; + } else { + return 2; + } +} diff --git a/specifelse/2.cpp b/specifelse/2.cpp new file mode 100644 index 0000000..4fdc1e4 --- /dev/null +++ b/specifelse/2.cpp @@ -0,0 +1,11 @@ +int slow(int x) { + if (x > 0) { + return 1; + } else { + return 0; + } +} + +int fast(int x) { + return (x > 0); +} diff --git a/specifelse/3.cpp b/specifelse/3.cpp new file mode 100644 index 0000000..09e9063 --- /dev/null +++ b/specifelse/3.cpp @@ -0,0 +1,11 @@ +int slow(int x) { + if (x > 0) { + return 42; + } else { + return 32; + } +} + +int fast(int x) { + return 32 + (x > 0) * 10; +} diff --git a/specifelse/4.cpp b/specifelse/4.cpp new file mode 100644 index 0000000..5ac0086 --- /dev/null +++ b/specifelse/4.cpp @@ -0,0 +1,47 @@ +#include +#include +#include "ticktock.h" +#include "randint.h" + +static int ifelse_clamp(int x) { + if (x < 0) { + return 0; + } else if (x > 255) { + return 1; + } else { + return x; + } +} + +static int bailan_clamp(int x) { + return (x < 0) ? 0 : ((x > 255) ? 1 : x); +} + +static int addmul_clamp(int x) { + return (x >= 0) * (x <= 255) * x + (x > 255); +} + +__attribute__((noinline)) void test(int *a, int n, int (*clamp)(int)) { + for (int i = 0; i < n; i++) { + a[i] = clamp(a[i]); + } +} + +int main() { + std::vector a((int)1e7); + + std::generate(a.begin(), a.end(), randint); + TICK(ifelse); + test(a.data(), a.size(), ifelse_clamp); + TOCK(ifelse); + + std::generate(a.begin(), a.end(), randint); + TICK(bailan); + test(a.data(), a.size(), bailan_clamp); + TOCK(bailan); + + std::generate(a.begin(), a.end(), randint); + TICK(addmul); + test(a.data(), a.size(), addmul_clamp); + TOCK(addmul); +} diff --git a/specifelse/randint.h b/specifelse/randint.h index f76ddde..4bf7abd 100644 --- a/specifelse/randint.h +++ b/specifelse/randint.h @@ -5,7 +5,13 @@ template static T randint(T minVal, T maxVal) { static std::mt19937 gen(0); - std::uniform_int_distribution uni(minVal, maxVal); + std::uniform_int_distribution uni(minVal, maxVal); return uni(gen); } +template +static T randint() { + static std::mt19937 gen(0); + std::uniform_int_distribution uni(minVal, maxVal); + return uni(gen); +} diff --git a/specifelse/slides.pptx b/specifelse/slides.pptx new file mode 100644 index 0000000..1bea888 Binary files /dev/null and b/specifelse/slides.pptx differ