Switch is faster.

mail@pastecode.io avatar
unknown
c_cpp
a month ago
3.5 kB
1
Indexable
Never
#include <iostream>
#include <chrono>

inline uint8_t switch_sqrt(uint8_t N) {
    switch(N) {
    case 0: return 0;
    case 1: case 2: case 3: return 1;
    case 4: case 5: case 6: case 7: case 8: return 2;
    case 9: case 10: case 11: case 12: case 13: case 14: case 15: return 3;
    case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23: case 24: return 4;
    case 25: case 26: case 27: case 28: case 29: case 30: case 31: case 32: case 33: case 34: case 35: return 5;
    case 36: case 37: case 38: case 39: case 40: case 41: case 42: case 43: case 44: case 45: case 46: case 47: case 48: return 6;
    case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: case 58: case 59: case 60: case 61: case 62: case 63: return 7;
    case 64: case 65: case 66: case 67: case 68: case 69: case 70: case 71: case 72: case 73: case 74: case 75: case 76: case 77: case 78: case 79: case 80: return 8;
    case 81: case 82: case 83: case 84: case 85: case 86: case 87: case 88: case 89: case 90: case 91: case 92: case 93: case 94: case 95: case 96: case 97: case 98: case 99: return 9;
    case 100: case 101: case 102: case 103: case 104: case 105: case 106: case 107: case 108: case 109: case 110: case 111: case 112: case 113: case 114: case 115: case 116: case 117: case 118: case 119: case 120: return 10;
    case 121: case 122: case 123: case 124: case 125: case 126: case 127: case 128: case 129: case 130: case 131: case 132: case 133: case 134: case 135: case 136: case 137: case 138: case 139: case 140: case 141: case 142: case 143: return 11;
    case 144: case 145: case 146: case 147: case 148: case 149: case 150: case 151: case 152: case 153: case 154: case 155: case 156: case 157: case 158: case 159: case 160: case 161: case 162: case 163: case 164: case 165: case 166: case 167: case 168: return 12;
    case 169: case 170: case 171: case 172: case 173: case 174: case 175: case 176: case 177: case 178: case 179: case 180: case 181: case 182: case 183: case 184: case 185: case 186: case 187: case 188: case 189: case 190: case 191: case 192: case 193: case 194: case 195: return 13;
    case 196: case 197: case 198: case 199: case 200: case 201: case 202: case 203: case 204: case 205: case 206: case 207: case 208: case 209: case 210: case 211: case 212: case 213: case 214: case 215: case 216: case 217: case 218: case 219: case 220: case 221: case 222: case 223: case 224: return 14;
    default: return 15;
    }
}

inline uint8_t if_sqrt(uint8_t N) {
    if (N < 1) return 0;
    if (N < 4) return 1;
    if (N < 9) return 2;
    if (N < 16) return 3;
    if (N < 25) return 4;
    if (N < 36) return 5;
    if (N < 49) return 6;
    if (N < 64) return 7;
    if (N < 81) return 8;
    if (N < 100) return 9;
    if (N < 121) return 10;
    if (N < 144) return 11;
    if (N < 169) return 12;
    if (N < 196) return 13;
    if (N < 225) return 14;
    return 15;
}

using hrc = std::chrono::high_resolution_clock;

int main() {
    uint8_t n1 = 0, n2 = 0;
    auto one = hrc::now();
    for (size_t i = 0; i < 1000000; i++) n1 += switch_sqrt(uint8_t(i));
    auto two = hrc::now();
    for (size_t i = 0; i < 1000000; i++) n2 += if_sqrt(uint8_t(i));
    auto three = hrc::now();

    std::cout << "switch: " << int(n1) << " "
              << (two - one).count() << std::endl;
    std::cout << "if:     " << int(n2) << " "
              << (three - two).count() << std::endl;

    return 0;
}
Leave a Comment