Interstellar12312

Custom insertion sort vs standard insertion sort with Cache clearing

Jul 20th, 2026
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <chrono>
  4. #include <random>
  5. #include <algorithm>
  6. #include <iomanip>
  7.  
  8. // Custom insertion sort vs standard insertion sort with Cache clearing
  9. // Contact Email [email protected]
  10. // Created by I.A
  11.  
  12. //Cache Clearing
  13. //Allocates 32MB of memory
  14. const size_t CACHE_SIZE_BYTES = 32 * 1024 * 1024;
  15. const size_t CACHE_SIZE_INTS = CACHE_SIZE_BYTES / sizeof(int);
  16. std::vector<int> cache_trasher(CACHE_SIZE_INTS, 0);
  17.  
  18. void flushCache() {
  19.     // Use volatile to prevent the compiler from optimizing this loop away
  20.     volatile int* p = cache_trasher.data();
  21.  
  22.     // Step by 16 integers (64 bytes) to touch every standard CPU cache line
  23.     for (size_t i = 0; i < CACHE_SIZE_INTS; i += 16) {
  24.         p[i] += 1;
  25.     }
  26. }
  27. // --------------------------------
  28.  
  29. void standardInsertionSort(std::vector<int>& arr) {
  30.     int n = arr.size();
  31.     for (int i = 1; i < n; i++) {
  32.         int key = arr[i];
  33.         int j = i - 1;
  34.         while (j >= 0 && arr[j] > key) {
  35.             arr[j + 1] = arr[j];
  36.             j--;
  37.         }
  38.         arr[j + 1] = key;
  39.     }
  40. }
  41.  
  42. void customJumpSort(std::vector<int>& arr) {
  43.     int n = arr.size();
  44.     for (int i = 1; i < n; i++) {
  45.         int key = arr[i];
  46.         int j = i - 1;
  47.         while (j >= 0 && arr[j] > key) {
  48.             if (j >= 1) {
  49.                 arr[j + 1] = arr[j];
  50.                 if (arr[j - 1] > key) {
  51.                     arr[j] = arr[j - 1];
  52.                     j -= 2;
  53.                 } else {
  54.                     arr[j] = key;
  55.                     goto placed;
  56.                 }
  57.             } else {
  58.                 arr[j + 1] = arr[j];
  59.                 j--;
  60.             }
  61.         }
  62.         arr[j + 1] = key;
  63.         placed:;
  64.     }
  65. }
  66.  
  67. int main() {
  68.     const int NUM_ELEMENTS = 32;
  69.     const int NUM_TRIALS = 100000;
  70.  
  71.     std::random_device rd;
  72.     std::mt19937 gen(rd());
  73.     std::uniform_int_distribution<> dist(1, 100000);
  74.  
  75.     double totalStd = 0, totalJump = 0;
  76.  
  77.     for (int t = 0; t < NUM_TRIALS; ++t) {
  78.         std::vector<int> data1(NUM_ELEMENTS);
  79.         for (int& x : data1) x = dist(gen);
  80.         std::vector<int> data2 = data1;
  81.  
  82.         // Flush Cache before Standard
  83.         flushCache();
  84.  
  85.         auto s1 = std::chrono::steady_clock::now();
  86.         standardInsertionSort(data1);
  87.         auto e1 = std::chrono::steady_clock::now();
  88.         totalStd += std::chrono::duration<double>(e1 - s1).count();
  89.  
  90.         // Flush Cache before Custom
  91.         flushCache();
  92.  
  93.         auto s2 = std::chrono::steady_clock::now();
  94.         customJumpSort(data2);
  95.         auto e2 = std::chrono::steady_clock::now();
  96.         totalJump += std::chrono::duration<double>(e2 - s2).count();
  97.  
  98.         // Verify Logic
  99.         if (!std::is_sorted(data2.begin(), data2.end())) {
  100.             std::cout << "CRITICAL ERROR: Custom Sort Failed!" << std::endl;
  101.             return 1;
  102.         }
  103.     }
  104.  
  105.     std::cout << std::left << std::setw(20) << "Algorithm" << " | " << "Total Time (s)" << std::endl;
  106.     std::cout << "--------------------------------------------" << std::endl;
  107.     std::cout << std::setw(20) << "Standard Insertion" << " | " << std::fixed << std::setprecision(6) << totalStd << std::endl;
  108.     std::cout << std::setw(20) << "Custom Jump Sort" << " | " << std::fixed << std::setprecision(6) << totalJump << std::endl;
  109.  
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment