Skip to content

Instantly share code, notes, and snippets.

@Silva97
Created July 8, 2020 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Silva97/230c6332c543fc670726e9d8fc0eb768 to your computer and use it in GitHub Desktop.
Save Silva97/230c6332c543fc670726e9d8fc0eb768 to your computer and use it in GitHub Desktop.
Teste de benchmark de um exemplo de código branchless para verificar qual número é menor
#include <stdio.h>
#include <stdlib.h>
#include "metric.h" // https://github.com/Silva97/metric
int smaller(int a, int b)
{
if (a < b) {
return a;
}
return b;
}
int smaller_branchless(int a, int b)
{
return a * (a < b) + b * (b <= a);
}
int main(void)
{
volatile int x = 5, y = 9;
METRIC_CALL(10000, smaller, x, y);
METRIC_CALL(10000, smaller_branchless, x, y);
return 0;
}
/*
┌─[~]
└─[felipe]─$ gcc tst.c -o tst
┌─[~]
└─[felipe]─$ ./tst
<BENCH> tst.c: smaller(x, y) x 10000
88 clocks, 0.00008700 secs
<BENCH> tst.c: smaller_branchless(x, y) x 10000
104 clocks, 0.00010300 secs
┌─[~]
└─[felipe]─$ ./tst
<BENCH> tst.c: smaller(x, y) x 10000
88 clocks, 0.00008600 secs
<BENCH> tst.c: smaller_branchless(x, y) x 10000
100 clocks, 0.00009800 secs
┌─[~]
└─[felipe]─$ ./tst
<BENCH> tst.c: smaller(x, y) x 10000
88 clocks, 0.00008700 secs
<BENCH> tst.c: smaller_branchless(x, y) x 10000
134 clocks, 0.00013200 secs
┌─[~]
└─[felipe]─$ ./tst
<BENCH> tst.c: smaller(x, y) x 10000
89 clocks, 0.00008700 secs
<BENCH> tst.c: smaller_branchless(x, y) x 10000
106 clocks, 0.00010400 secs
┌─[~]
└─[felipe]─$
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment