Profile
Pastes: 74262
Members: 1437

Paste

Plain view | Edit code: here. | Add this to your website. | Report abuse.

Short URL: http://goo.gl/hfi7N

Pasted as C++ by porgarmingduod on Friday, October 7th, 2011 7:16am ( 3 years ago )

  1. #include <cstdint>
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <boost/chrono.hpp>
  5. #include <boost/random/mersenne_twister.hpp>
  6. #include <boost/random/uniform_int_distribution.hpp>
  7.  
  8. #include "stdint.h"
  9. #include <stdlib.h>
  10.  
  11. boost::random::mt19937 rng;
  12. boost::random::uniform_int_distribution<> ranchar(0, 255);
  13.  
  14. double normal(uint64_t runs) {
  15.   uint64_t result = 0;
  16.   rng.seed(0);
  17.  
  18.   boost::chrono::high_resolution_clock::time_point start =
  19.     boost::chrono::high_resolution_clock::now();
  20.   for (; runs; runs--) {
  21.     int a = ranchar(rng);
  22.     int b = ranchar(rng);
  23.     int c = ranchar(rng);
  24.     int d = ranchar(rng);
  25.     if (a < 16 or b < 32 or c < 64 or d < 128) result += a;
  26.     if (d > 16 or c > 32 or b > 64 or a > 128) result += b;
  27.     if (a < 96 or b < 53 or c < 199 or d < 177) result += c;
  28.     if (d > 66 or c > 35 or b > 99 or a > 77) result += d;
  29.   }
  30.  
  31.   std::cout << "Result check " << result << std::endl;
  32.   boost::chrono::duration<double> sec =
  33.     boost::chrono::high_resolution_clock::now() - start;
  34.   return sec.count();
  35. }
  36. double special(uint64_t runs) {
  37.   uint64_t result = 0;
  38.   rng.seed(0);
  39.  
  40.   boost::chrono::high_resolution_clock::time_point start = boost::chrono::high_resolution_clock::now();
  41.   for (; runs; runs--) {
  42.     int a = ranchar(rng);
  43.     int b = ranchar(rng);
  44.     int c = ranchar(rng);
  45.     int d = ranchar(rng);
  46.     if (a < 16 | b < 32 | c < 64 | d < 128) result += a;
  47.     if (d > 16 | c > 32 | b > 64 | a > 128) result += b;
  48.     if (a < 96 | b < 53 | c < 199 | d < 177) result += c;
  49.     if (d > 66 | c > 35 | b > 99 | a > 77) result += d;
  50.   }
  51.  
  52.   std::cout << "Result check " << result << std::endl;
  53.   boost::chrono::duration<double> sec = boost::chrono::high_resolution_clock::now() - start;
  54.   return sec.count();
  55. }
  56.  
  57. double collapsed(uint64_t runs) {
  58.   uint64_t result = 0;
  59.   rng.seed(0);
  60.  
  61.   boost::chrono::high_resolution_clock::time_point start = boost::chrono::high_resolution_clock::now();
  62.  
  63.   for (; runs; runs--) {
  64.     int a = ranchar(rng);
  65.     int b = ranchar(rng);
  66.     int c = ranchar(rng);
  67.     int d = ranchar(rng);
  68.     result += a * (a < 16 | b < 32 | c < 64 | d < 128);
  69.     result += b * (d > 16 | c > 32 | b > 64 | a > 128);
  70.     result += c * (a < 96 | b < 53 | c < 199 | d < 177);
  71.     result += d * (d > 66 | c > 35 | b > 99 | a > 77);
  72.   }
  73.   std::cout << "Result check " << result << std::endl;
  74.   boost::chrono::duration<double> sec = boost::chrono::high_resolution_clock::now() - start;
  75.   return sec.count();
  76. }
  77.  
  78. int main() {
  79.   double best_normal = 999999.0;
  80.   double best_collapsed = 999999.0;
  81.   double best_special = 999999.0;
  82.   for (int x = 0; x < 25; x++) {
  83.     double result = normal(5000000);
  84.     std::cout << "normal " << result << std::endl;
  85.     if (result < best_normal) best_normal = result;
  86.     result = collapsed(5000000);
  87.     std::cout << "collapsed " << result << std::endl;
  88.     if (result < best_collapsed) best_collapsed = result;
  89.     result = special(5000000);
  90.     std::cout << "special if " << result << std::endl;
  91.     if (result < best_special) best_special = result;
  92.   }
  93.   std::cout << "Normal if best: " << best_normal << ", Special if best: " << best_special << ", Collapsed if best: " << best_collapsed << std::endl;
  94. }

Revise this Paste
Parent: 39295
Your Name:
Code Language:
 
Security Image:
Text seen in Image:
Comments

Nothing has been added as yet. Post a comment.