Skip to content

Instantly share code, notes, and snippets.

@lambday
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lambday/73b9e68ec08e2da25967 to your computer and use it in GitHub Desktop.
Save lambday/73b9e68ec08e2da25967 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdlib>
using namespace std;
typedef double float64_t;
typedef unsigned long index_t;
template <class T>
struct ComputeSquare
{
T compute(T n)
{
return n*n;
}
protected:
~ComputeSquare() {}
};
template <class T>
struct ComputeCube
{
T compute(T n)
{
return n*n*n;
}
protected:
~ComputeCube() {}
};
template <template <class> class Compute>
struct Computation : public Compute<float64_t>
{
};
int main(int argc, char** argv)
{
if (argc < 2)
exit(1);
const index_t iter = atol(argv[1]);
Computation<ComputeSquare>* sq = new Computation<ComputeSquare>();
for (index_t i = 1; i <= iter; ++i)
{
sq->compute(i-1);
sq->compute(i);
sq->compute(i+1);
}
delete sq;
Computation<ComputeCube>* cube = new Computation<ComputeCube>();
for (index_t i = 1; i <= iter; ++i)
{
cube->compute(i-1);
cube->compute(i);
cube->compute(i+1);
}
delete cube;
return 0;
}
[lambday@lambday.iitb.ac.in policy_virtual]$ time ./a.out 10000000000
real 0m3.203s
user 0m3.200s
sys 0m0.000s
#include <iostream>
#include <cstdlib>
using namespace std;
typedef double float64_t;
typedef unsigned long index_t;
template <class T>
struct Computation
{
virtual T compute(T n) = 0;
virtual ~Computation() {}
};
struct ComputeSquare : public Computation<float64_t>
{
virtual float64_t compute(float64_t n)
{
return n*n;
}
virtual ~ComputeSquare() {}
};
struct ComputeCube : public Computation<float64_t>
{
virtual float64_t compute(float64_t n)
{
return n*n*n;
}
virtual ~ComputeCube() {}
};
int main(int argc, char** argv)
{
if (argc < 2)
exit(1);
const index_t iter = atol(argv[1]);
Computation<float64_t>* sq = new ComputeSquare();
for (index_t i = 1; i <= iter; ++i)
{
sq->compute(i-1);
sq->compute(i);
sq->compute(i+1);
}
delete sq;
Computation<float64_t>* cube = new ComputeCube();
for (index_t i = 1; i <= iter; ++i)
{
cube->compute(i-1);
cube->compute(i);
cube->compute(i+1);
}
delete cube;
return 0;
}
[lambday@lambday.iitb.ac.in policy_virtual]$ time ./a.out 10000000000
real 3m20.989s
user 3m20.904s
sys 0m0.002s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment