Welcome, guest! Login / Register - Why register?
[ Pastes: 81186 ] [ Members: 2078 ]

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 ( 5 years ago )

#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <boost/chrono.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>

#include "stdint.h"
#include <stdlib.h>

boost::random::mt19937 rng;
boost::random::uniform_int_distribution<> ranchar(0, 255);

double normal(uint64_t runs) {
  uint64_t result = 0;
  rng.seed(0);
  
  boost::chrono::high_resolution_clock::time_point start = 
    boost::chrono::high_resolution_clock::now();
  for (; runs; runs--) {
    int a = ranchar(rng);
    int b = ranchar(rng);
    int c = ranchar(rng);
    int d = ranchar(rng);
    if (a < 16 or b < 32 or c < 64 or d < 128) result += a;
    if (d > 16 or c > 32 or b > 64 or a > 128) result += b;
    if (a < 96 or b < 53 or c < 199 or d < 177) result += c;
    if (d > 66 or c > 35 or b > 99 or a > 77) result += d;
  }
  
  std::cout << "Result check " << result << std::endl;
  boost::chrono::duration<double> sec = 
    boost::chrono::high_resolution_clock::now() - start;
  return sec.count();
}
double special(uint64_t runs) {
  uint64_t result = 0;
  rng.seed(0);
  
  boost::chrono::high_resolution_clock::time_point start = boost::chrono::high_resolution_clock::now();
  for (; runs; runs--) {
    int a = ranchar(rng);
    int b = ranchar(rng);
    int c = ranchar(rng);
    int d = ranchar(rng);
    if (a < 16 | b < 32 | c < 64 | d < 128) result += a;
    if (d > 16 | c > 32 | b > 64 | a > 128) result += b;
    if (a < 96 | b < 53 | c < 199 | d < 177) result += c;
    if (d > 66 | c > 35 | b > 99 | a > 77) result += d;
  }
  
  std::cout << "Result check " << result << std::endl;
  boost::chrono::duration<double> sec = boost::chrono::high_resolution_clock::now() - start;
  return sec.count();
}

double collapsed(uint64_t runs) {
  uint64_t result = 0;
  rng.seed(0);
  
  boost::chrono::high_resolution_clock::time_point start = boost::chrono::high_resolution_clock::now();
  
  for (; runs; runs--) {
    int a = ranchar(rng);
    int b = ranchar(rng);
    int c = ranchar(rng);
    int d = ranchar(rng);
    result += a * (a < 16 | b < 32 | c < 64 | d < 128);
    result += b * (d > 16 | c > 32 | b > 64 | a > 128);
    result += c * (a < 96 | b < 53 | c < 199 | d < 177);
    result += d * (d > 66 | c > 35 | b > 99 | a > 77);
  }
  std::cout << "Result check " << result << std::endl;
  boost::chrono::duration<double> sec = boost::chrono::high_resolution_clock::now() - start;
  return sec.count();
}

int main() {
  double best_normal = 999999.0;
  double best_collapsed = 999999.0;
  double best_special = 999999.0;
  for (int x = 0; x < 25; x++) {
    double result = normal(5000000);
    std::cout << "normal " << result << std::endl;
    if (result < best_normal) best_normal = result;
    result = collapsed(5000000);
    std::cout << "collapsed " << result << std::endl;
    if (result < best_collapsed) best_collapsed = result;
    result = special(5000000);
    std::cout << "special if " << result << std::endl;
    if (result < best_special) best_special = result;
  }
  std::cout << "Normal if best: " << best_normal << ", Special if best: " << best_special << ", Collapsed if best: " << best_collapsed << std::endl;
}

Revise this Paste

Parent: 39295
Your Name:
Code Language:

To minimise the influx of spam: you are required to register to post any code.