Advertisement
runewalsh

Кукаретики соснулей [2]

Nov 29th, 2012
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.53 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include "math.h"
  5. #include <string>
  6. #pragma warning(disable:4996)
  7.  
  8. class Timer
  9. {
  10. public:
  11.     void initialize()
  12.     {
  13.         LARGE_INTEGER qpcFreq;
  14.         QueryPerformanceFrequency(&qpcFreq);
  15.         invFreq = 1.0 / (double)qpcFreq.QuadPart;
  16.     }
  17.     double time()
  18.     {
  19.         LARGE_INTEGER counter;
  20.         QueryPerformanceCounter(&counter);
  21.         return (double)counter.QuadPart * invFreq;
  22.     }
  23. private:
  24.     double invFreq;
  25. };
  26.  
  27. Timer timer = Timer();
  28. FILE *fnull = fopen("nul", "wb");
  29.  
  30. const float Pi = 3.14159265358979323846f;
  31. const float TwoPi = 2.0f * Pi;
  32. typedef unsigned int uint_t;
  33.  
  34. class TriTable
  35. {
  36. private:
  37.     uint_t size;
  38.     float invSize;
  39.     float *vsin, *vcos, *vth;
  40. public:
  41.     TriTable(uint_t newSize)
  42.     {
  43.         size = newSize;
  44.         invSize = 1.0f / (float)size;
  45.         vsin = new float[size];
  46.         vcos = new float[size];
  47.         vth = new float[size];
  48.     }
  49.  
  50.     ~TriTable()
  51.     {
  52.         delete[] vsin;
  53.         delete[] vcos;
  54.         delete[] vth;
  55.     }
  56.  
  57.     void calculate(float shift = 0.0f)
  58.     {
  59.         for (uint_t i = 0; i < size; i++)
  60.         {
  61.             float x = shift + (float)i * invSize * TwoPi;
  62.             vsin[i] = sinf(x);
  63.             vcos[i] = cosf(x);
  64.             vth[i] = tanh(x);
  65.         }
  66.     }
  67.  
  68.     void print(int nsteps)
  69.     {
  70.         float invnsteps = 1.0f / (float)nsteps;
  71.  
  72.         for (int i = 0; i < nsteps; i++)
  73.         {
  74.             int x = (int)((float)size * (float)i * invnsteps);
  75.             fprintf(fnull, "%.3f %.3f %.3f\n", vsin[x], vcos[x], vth[x]);
  76.         }
  77.     }
  78. };
  79.  
  80. const int MAX_TRITABLES = 10;
  81.  
  82. class TriTable_S
  83. {
  84. private:
  85.     static int nextId;
  86.     static uint_t *size;
  87.     static float *invSize;
  88.     static float **vsin, **vcos, **vth;
  89.     int id;
  90. public:
  91.     TriTable_S(int newSize)
  92.     {
  93.         id = nextId;
  94.         nextId = (nextId + 1) % MAX_TRITABLES;
  95.  
  96.         size[id] = newSize;
  97.         invSize[id] = 1.0f / (float)size[id];
  98.         vsin[id] = new float[size[id]];
  99.         vcos[id] = new float[size[id]];
  100.         vth[id] = new float[size[id]];
  101.     }
  102.  
  103.     ~TriTable_S()
  104.     {
  105.         delete[] vsin[id];
  106.         delete[] vcos[id];
  107.         delete[] vth[id];
  108.     }
  109.  
  110.     void calculate(float shift = 0.0f)
  111.     {
  112.         for (uint_t i = 0; i < size[id]; i++)
  113.         {
  114.             float x = shift + (float)i * invSize[id] * TwoPi;
  115.             vsin[id][i] = sinf(x);
  116.             vcos[id][i] = cosf(x);
  117.             vth[id][i] = tanh(x);
  118.         }
  119.     }
  120.  
  121.     void print(int nsteps)
  122.     {
  123.         float invnsteps = 1.0f / (float)nsteps;
  124.  
  125.         for (int i = 0; i < nsteps; i++)
  126.         {
  127.             int x = (int)((float)size[id] * (float)i * invnsteps);
  128.             fprintf(fnull, "%.3f %.3f %.3f\n", vsin[id][x], vcos[id][x], vth[id][x]);
  129.         }
  130.     }
  131. };
  132.  
  133. int TriTable_S::nextId = 0;
  134. uint_t *TriTable_S::size = new uint_t[MAX_TRITABLES];
  135. float
  136.     *TriTable_S::invSize = new float[MAX_TRITABLES],
  137.     **TriTable_S::vsin = new float*[MAX_TRITABLES],
  138.     **TriTable_S::vcos = new float*[MAX_TRITABLES],
  139.     **TriTable_S::vth = new float*[MAX_TRITABLES];
  140.  
  141. const uint_t TABLES = 5;
  142. const uint_t ITERATIONS = 100;
  143. const float INV_ITERATIONS = 1.0f / (float)ITERATIONS;
  144. const uint_t TABLE_SIZE = 100000;
  145.  
  146. template <class T>
  147. void test(const std::string& name)
  148. {
  149.     T **tables = new T*[TABLES];
  150.     for (uint_t i = 0; i < TABLES; i++)
  151.         tables[i] = new T(TABLE_SIZE);
  152.  
  153.     printf("%s: ", name.c_str());
  154.     double time = timer.time();
  155.  
  156.     for (uint_t i = 0; i < ITERATIONS; i++)
  157.     {
  158.         int tid = i % TABLES;
  159.         tables[tid]->calculate((float)i * INV_ITERATIONS * TwoPi);
  160.         tables[tid]->print(2 + i%5);
  161.     }
  162.  
  163.     time = timer.time() - time;
  164.     printf("%lf ms\n", time * INV_ITERATIONS * 1000.0);
  165.    
  166.     for (uint_t i = 0; i < TABLES; i++)
  167.         delete tables[i];
  168.     delete tables;
  169. };
  170.  
  171. int main()
  172. {
  173.     timer.initialize();
  174.  
  175.     test<TriTable>("TriTable");
  176.     test<TriTable_S>("TriTable_S");
  177.  
  178.     return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement