Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <windows.h>
- #include <stdio.h>
- #include "math.h"
- #include <string>
- #pragma warning(disable:4996)
- class Timer
- {
- public:
- void initialize()
- {
- LARGE_INTEGER qpcFreq;
- QueryPerformanceFrequency(&qpcFreq);
- invFreq = 1.0 / (double)qpcFreq.QuadPart;
- }
- double time()
- {
- LARGE_INTEGER counter;
- QueryPerformanceCounter(&counter);
- return (double)counter.QuadPart * invFreq;
- }
- private:
- double invFreq;
- };
- Timer timer = Timer();
- FILE *fnull = fopen("nul", "wb");
- const float Pi = 3.14159265358979323846f;
- const float TwoPi = 2.0f * Pi;
- typedef unsigned int uint_t;
- class TriTable
- {
- private:
- uint_t size;
- float invSize;
- float *vsin, *vcos, *vth;
- public:
- TriTable(uint_t newSize)
- {
- size = newSize;
- invSize = 1.0f / (float)size;
- vsin = new float[size];
- vcos = new float[size];
- vth = new float[size];
- }
- ~TriTable()
- {
- delete[] vsin;
- delete[] vcos;
- delete[] vth;
- }
- void calculate(float shift = 0.0f)
- {
- for (uint_t i = 0; i < size; i++)
- {
- float x = shift + (float)i * invSize * TwoPi;
- vsin[i] = sinf(x);
- vcos[i] = cosf(x);
- vth[i] = tanh(x);
- }
- }
- void print(int nsteps)
- {
- float invnsteps = 1.0f / (float)nsteps;
- for (int i = 0; i < nsteps; i++)
- {
- int x = (int)((float)size * (float)i * invnsteps);
- fprintf(fnull, "%.3f %.3f %.3f\n", vsin[x], vcos[x], vth[x]);
- }
- }
- };
- const int MAX_TRITABLES = 10;
- class TriTable_S
- {
- private:
- static int nextId;
- static uint_t *size;
- static float *invSize;
- static float **vsin, **vcos, **vth;
- int id;
- public:
- TriTable_S(int newSize)
- {
- id = nextId;
- nextId = (nextId + 1) % MAX_TRITABLES;
- size[id] = newSize;
- invSize[id] = 1.0f / (float)size[id];
- vsin[id] = new float[size[id]];
- vcos[id] = new float[size[id]];
- vth[id] = new float[size[id]];
- }
- ~TriTable_S()
- {
- delete[] vsin[id];
- delete[] vcos[id];
- delete[] vth[id];
- }
- void calculate(float shift = 0.0f)
- {
- for (uint_t i = 0; i < size[id]; i++)
- {
- float x = shift + (float)i * invSize[id] * TwoPi;
- vsin[id][i] = sinf(x);
- vcos[id][i] = cosf(x);
- vth[id][i] = tanh(x);
- }
- }
- void print(int nsteps)
- {
- float invnsteps = 1.0f / (float)nsteps;
- for (int i = 0; i < nsteps; i++)
- {
- int x = (int)((float)size[id] * (float)i * invnsteps);
- fprintf(fnull, "%.3f %.3f %.3f\n", vsin[id][x], vcos[id][x], vth[id][x]);
- }
- }
- };
- int TriTable_S::nextId = 0;
- uint_t *TriTable_S::size = new uint_t[MAX_TRITABLES];
- float
- *TriTable_S::invSize = new float[MAX_TRITABLES],
- **TriTable_S::vsin = new float*[MAX_TRITABLES],
- **TriTable_S::vcos = new float*[MAX_TRITABLES],
- **TriTable_S::vth = new float*[MAX_TRITABLES];
- const uint_t TABLES = 5;
- const uint_t ITERATIONS = 100;
- const float INV_ITERATIONS = 1.0f / (float)ITERATIONS;
- const uint_t TABLE_SIZE = 100000;
- template <class T>
- void test(const std::string& name)
- {
- T **tables = new T*[TABLES];
- for (uint_t i = 0; i < TABLES; i++)
- tables[i] = new T(TABLE_SIZE);
- printf("%s: ", name.c_str());
- double time = timer.time();
- for (uint_t i = 0; i < ITERATIONS; i++)
- {
- int tid = i % TABLES;
- tables[tid]->calculate((float)i * INV_ITERATIONS * TwoPi);
- tables[tid]->print(2 + i%5);
- }
- time = timer.time() - time;
- printf("%lf ms\n", time * INV_ITERATIONS * 1000.0);
- for (uint_t i = 0; i < TABLES; i++)
- delete tables[i];
- delete tables;
- };
- int main()
- {
- timer.initialize();
- test<TriTable>("TriTable");
- test<TriTable_S>("TriTable_S");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement