Last active
January 20, 2020 02:55
-
-
Save kghose/19838ecd85b7b63fc5c7fcf53015efe1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
struct Feet { | |
double f; | |
Feet(double f) | |
: f(f) | |
{ | |
} | |
explicit constexpr operator double() const { return f; } | |
}; | |
struct Meters { | |
double m; | |
constexpr Meters(double m) | |
: m(m) | |
{ | |
} | |
constexpr Meters(const Feet& f) | |
: m(0.3048 * f.f) | |
{ | |
} | |
constexpr Meters& operator+=(const Meters& rhs) | |
{ | |
m += rhs.m; | |
return *this; | |
} | |
explicit constexpr operator double() const { return m; } | |
}; | |
Meters foo(Meters m1) { return Meters{ 2 * m1.m }; } | |
int main(int argc, char* argv[]) | |
{ | |
size_t N = 10000000; | |
std::vector<Feet> feet; | |
feet.reserve(N); | |
for (int i = 0; i < N; i++) { | |
feet.push_back(i); | |
} | |
Meters sum = 0.0; | |
for (int i = 0; i < N; i++) { | |
sum += foo(feet[i]); | |
} | |
std::cout << double(sum); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
typedef double Meters; | |
typedef double Feet; | |
Meters foo(Meters m1) { return Meters{ 2 * m1 }; } | |
int main(int argc, char* argv[]) | |
{ | |
size_t N = 10000000; | |
std::vector<Feet> feet; | |
feet.reserve(N); | |
for (int i = 0; i < N; i++) { | |
feet.push_back(i); | |
} | |
Meters sum = 0.0; | |
for (int i = 0; i < N; i++) { | |
sum += foo(0.3048 * feet[i]); | |
} | |
std::cout << sum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment