Last active
September 21, 2025 08:23
-
-
Save jweinst1/526c571fd137665d634002c49f4f7f7a to your computer and use it in GitHub Desktop.
Bench mark of neighbor iteration in submasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <array> | |
| #include <vector> | |
| #include <cstring> | |
| #include <cstdlib> | |
| #include <cstdio> | |
| #include <ctime> | |
| #include <cmath> | |
| #include <algorithm> | |
| #include <iostream> | |
| #include <random> | |
| #include <numeric> | |
| #include <limits> | |
| #include <cstdint> | |
| #include <chrono> | |
| #include <bitset> | |
| #include <stdio.h> | |
| #include <stdint.h> | |
| static uint8_t determineExponent(uint8_t quant) { | |
| #if defined(__GNUC__) || defined(__clang__) | |
| return quant ? 31 - __builtin_clz(quant) : 0; | |
| #else | |
| if (quant & (1 << 7)) | |
| return 7; | |
| if (quant & (1 << 6)) | |
| return 6; | |
| if (quant & (1 << 5)) | |
| return 5; | |
| if (quant & (1 << 4)) | |
| return 4; | |
| if (quant & (1 << 3)) | |
| return 3; | |
| if (quant & (1 << 2)) | |
| return 2; | |
| if (quant & (1 << 1)) | |
| return 1; | |
| return 0; | |
| #endif | |
| } | |
| static void fooTest() { | |
| uint8_t total = 0b101100; | |
| uint8_t field = total | (total - 1) | (total + 1); | |
| std::cout << "base " << std::bitset<8>(total) << " field " << std::bitset<8>(field) << "\n"; | |
| uint8_t diff = total ^ field; | |
| std::cout << "diff " << std::bitset<8>(diff) << "\n"; | |
| uint8_t other = 0b111101; | |
| uint8_t other_diff = other ^ field; | |
| std::cout << "other " << std::bitset<8>(other) << " other_diff " << std::bitset<8>(other_diff) << "\n"; | |
| std::cout << "checking " << std::bitset<8>((diff ^ other_diff) & ~diff) << "\n"; | |
| std::cout << "test " << std::bitset<8>(0b11111011 ^ 0b100) << "\n"; | |
| } | |
| static void printByte(const char* prefix, uint8_t byte) { | |
| std::cout << prefix << " " << std::bitset<8>(byte) << "\n"; | |
| } | |
| // Iterate through all submasks of mask | |
| void iterate_submasks(uint8_t mask) { | |
| // Start from the mask itself, then decrease | |
| for (uint8_t sub = mask; sub; sub = (sub - 1) & mask) { | |
| printf("0b"); | |
| for (int i = 7; i >= 0; --i) { | |
| printf("%d", (sub >> i) & 1); | |
| } | |
| printf("\n"); | |
| } | |
| } | |
| uint8_t iterate_mash_set(uint8_t set, uint8_t key, uint8_t* iter) { | |
| return 0; | |
| } | |
| /** | |
| myInt is 213? | |
| Finished down in 599us | |
| myInt is 184 | |
| Finished up in 571us | |
| * */ | |
| int main(int argc, char const *argv[]) | |
| { | |
| // downwards iteration of neighbors in a byte set | |
| volatile uint8_t myInt = 0b11010101; | |
| uint8_t cur = myInt; | |
| auto start = std::chrono::high_resolution_clock::now(); | |
| for (int i = 0; i < 1000000; ++i) | |
| { | |
| cur = (cur - 1) & myInt; | |
| cur = myInt; | |
| } | |
| auto end = std::chrono::high_resolution_clock::now(); | |
| auto t8 = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count(); | |
| std::cout << "myInt is " << (size_t)myInt << cur << "\n"; | |
| std::cout << "Finished down in " << t8 << "us\n"; | |
| volatile uint8_t myInt2 = 0b10111000; | |
| uint8_t cur2 = 0; | |
| auto start2 = std::chrono::high_resolution_clock::now(); | |
| for (int i = 0; i < 1000000; ++i) | |
| { | |
| cur2 = (cur2 + 0b1000); | |
| myInt2 = 0b10111000; | |
| } | |
| auto end2 = std::chrono::high_resolution_clock::now(); | |
| auto t82 = std::chrono::duration_cast<std::chrono::microseconds>(end2 - start2).count(); | |
| std::cout << "myInt is " << (size_t)myInt2 << cur2 << "\n"; | |
| std::cout << "Finished up in " << t82 << "us\n"; | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| myInt is 967471917669810175 | |
| Finished down in 334us | |
| * */ | |
| int main(int argc, char const *argv[]) | |
| { | |
| // downwards iteration of neighbors in a byte set | |
| uint64_t set = 0b10101001010011101100101; | |
| uint64_t vect = (((uint64_t)13 << 56) | ((uint64_t)109 << 48) | ((uint64_t)38 << 40) | ((uint64_t)149 << 32)|uint64_t(78 << 24)|uint64_t(198 << 16) |uint64_t(130 << 8) |230); | |
| int i = 1000000; | |
| auto start = std::chrono::high_resolution_clock::now(); | |
| while(--i) | |
| { | |
| set |= (++vect); | |
| } | |
| auto end = std::chrono::high_resolution_clock::now(); | |
| auto t8 = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count(); | |
| std::cout << "myInt is " << (size_t)set << "\n"; | |
| std::cout << "Finished down in " << t8 << "us\n"; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment