#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define MAX_COUNT (1 << 29)
size_t counter = 0;
size_t testSwitch()
{
size_t i;
for (i = 0; i < MAX_COUNT; i++)
{
switch (counter % 4 + 1)
{
case 1: counter += 4; break;
case 2: counter += 3; break;
case 3: counter += 2; break;
case 4: counter += 1; break;
}
}
return 1000 * (clock() - start
) / CLOCKS_PER_SEC
; }
size_t testIf()
{
size_t i;
for (i = 0; i < MAX_COUNT; i++)
{
const size_t c = counter % 4 + 1;
if (c == 1) { counter += 4; }
else if (c == 2) { counter += 3; }
else if (c == 3) { counter += 2; }
else if (c == 4) { counter += 1; }
}
return 1000 * (clock() - start
) / CLOCKS_PER_SEC
; }
int main()
{
printf("Switch statement: %u ms\n", testSwitch
()); printf("If statement: %u ms\n", testIf
()); }
I2luY2x1ZGUgPHN0ZGxpYi5oPgojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPHRpbWUuaD4KCiNkZWZpbmUgTUFYX0NPVU5UICgxIDw8IDI5KQpzaXplX3QgY291bnRlciA9IDA7CgpzaXplX3QgdGVzdFN3aXRjaCgpCnsKICAgIGNsb2NrX3Qgc3RhcnQgPSBjbG9jaygpOwogICAgc2l6ZV90IGk7CiAgICBmb3IgKGkgPSAwOyBpIDwgTUFYX0NPVU5UOyBpKyspCiAgICB7CiAgICAgICAgc3dpdGNoIChjb3VudGVyICUgNCArIDEpCiAgICAgICAgewogICAgICAgICAgICBjYXNlIDE6IGNvdW50ZXIgKz0gNDsgYnJlYWs7CiAgICAgICAgICAgIGNhc2UgMjogY291bnRlciArPSAzOyBicmVhazsKICAgICAgICAgICAgY2FzZSAzOiBjb3VudGVyICs9IDI7IGJyZWFrOwogICAgICAgICAgICBjYXNlIDQ6IGNvdW50ZXIgKz0gMTsgYnJlYWs7CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIDEwMDAgKiAoY2xvY2soKSAtIHN0YXJ0KSAvIENMT0NLU19QRVJfU0VDOwp9CgpzaXplX3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