Advertisement
Guest User

pow

a guest
Jul 8th, 2012
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. // sploit.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <Windows.h>
  8. #include <cmath>
  9.  
  10.  
  11. static double LibPow(double x, int n)
  12. {
  13.     if (n < 0)
  14.     { return (1.0 / LibPow(x, -n)); }
  15.  
  16.     switch (n)
  17.     {
  18.         case 0:
  19.         { return (1.0); }
  20.         case 1:
  21.         { return (x); }
  22.         case 2:
  23.         { return (x * x); }
  24.         case 3:
  25.         { return (x * x * x); }
  26.         case 4:
  27.         {
  28.             double x2 = x * x;
  29.             return (x2 * x2);
  30.         }                case 5:
  31.         {
  32.             double x2 = x * x;
  33.             return (x2 * x2 * x);
  34.         }
  35.         case 6:
  36.         {
  37.             double x2 = x * x;
  38.             return (x2 * x2 * x2);
  39.         }
  40.         case 7:
  41.         {
  42.             double x3 = x * x * x;
  43.             return (x3 * x3 * x);
  44.         }
  45.         case 8:
  46.         {
  47.             double x2 = x * x;
  48.             double x4 = x2 * x2;
  49.             return (x4 * x4);
  50.         }
  51.         case 9:
  52.         {
  53.             double x3 = x * x * x;
  54.             return (x3 * x3 * x3);
  55.         }
  56.         case 10:
  57.         {
  58.             double x2 = x * x;
  59.             double x4 = x2 * x2;
  60.             return (x4 * x4 * x2);
  61.         }
  62.         default:
  63.         { return std::pow(x, n); }
  64.     }
  65. }
  66.  
  67. static double LibIfPow(double x, int n)
  68. {
  69.     if (n < 0)
  70.     { return (1.0 / LibPow(x, -n)); }
  71.  
  72.     if(n == 0) return (1.0);
  73.     else if(n == 1) return (x);
  74.     else if(n == 2) return (x * x);
  75.     else if(n == 3) return (x * x * x);
  76.     else if(n == 4) {
  77.             double x2 = x * x;
  78.             return (x2 * x2);
  79.     }
  80.     else if(n == 5) {
  81.             double x2 = x * x;
  82.             return (x2 * x2 * x);
  83.     } else if(n == 6) {
  84.             double x2 = x * x;
  85.             return (x2 * x2 * x2);
  86.     } else if(n == 7) {
  87.             double x3 = x * x * x;
  88.             return (x3 * x3 * x);
  89.     }
  90.     else if(n == 8) {
  91.             double x2 = x * x;
  92.             double x4 = x2 * x2;
  93.             return (x4 * x4);
  94.     }
  95.     else if(n == 9) {
  96.             double x3 = x * x * x;
  97.             return (x3 * x3 * x3);
  98.     } else if(n == 10) {
  99.             double x2 = x * x;
  100.             double x4 = x2 * x2;
  101.             return (x4 * x4 * x2);
  102.     } else { return std::pow(x, n); }
  103.    
  104. }
  105.  
  106. #define METHOD 3
  107. #define LIMIT 11
  108. int _tmain(int argc, _TCHAR* argv[])
  109. {
  110.     DWORD64 begin = __rdtsc();
  111.     double sum = 0;
  112.     for(int i = 0; i < 100000000; ++i)
  113.     {
  114. #if METHOD == 1
  115.         sum += LibPow(112.3, rand() % LIMIT);
  116.  
  117. #else
  118. #if METHOD == 2
  119.         sum += LibIfPow(112.3, rand() % LIMIT);
  120. #else
  121.         sum += std::pow(112.3, rand() % LIMIT);
  122. #endif
  123. #endif
  124.     }
  125.  
  126.     DWORD64 end = __rdtsc();
  127.     DWORD64 diff = end-begin;
  128.  
  129.     LARGE_INTEGER res;
  130.     QueryPerformanceFrequency(&res);
  131.     printf("\n%llu %llu", diff, diff/res.QuadPart);
  132.     return sum;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement