Serving the Quantitative Finance Community

 
User avatar
Cuchulainn
Posts: 20254
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

C++ quiz --- generic programming

January 13th, 2015, 9:11 am

I am trying to define "f > g" for two std::function instances(which means one can define constraints). I tried this, but to no avail:Any ideas?
Last edited by Cuchulainn on January 12th, 2015, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 20254
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

C++ quiz --- generic programming

June 11th, 2016, 8:44 am

Here is C++11 to test if a class B is a real interface and that D is its implementation.(CRTP solution).BTW C++ does not support interfaces, hence the work around.All errors are compile errors.Is it portable?
Last edited by Cuchulainn on June 10th, 2016, 10:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 20254
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++ quiz --- generic programming

February 4th, 2017, 10:21 am

Is it better to use std::move() or std::move_backward()?
 
User avatar
Cuchulainn
Posts: 20254
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++ quiz --- generic programming

February 4th, 2017, 10:28 am

Is it better to use std::move() or std::move_backward()?
 
User avatar
outrun
Posts: 4573
Joined: January 1st, 1970, 12:00 am

Re: C++ quiz --- generic programming

February 4th, 2017, 11:18 am

Hmmm: can you specify the measure(s) you want to compare them against (maintainability, compatibility, speed, binary size,...), as well as the scope in which you use it?

Eg: Move saves you from typing 9 extra characters so it's better for those who hate typing.
 
User avatar
outrun
Posts: 4573
Joined: January 1st, 1970, 12:00 am

Re: C++ quiz --- generic programming

February 4th, 2017, 11:29 am

I am trying to define "f > g" for two std::function instances(which means one can define constraints). I tried this, but to no avail:Any ideas?
maybe this argument? http://stackoverflow.com/a/3630174 
 
User avatar
Cuchulainn
Posts: 20254
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++ quiz --- generic programming

February 4th, 2017, 7:18 pm

Hmmm: can you specify the measure(s) you want to compare them against (maintainability, compatibility, speed, binary size,...), as well as the scope in which you use it?

Eg: Move saves you from typing 9 extra characters so it's better for those who hate typing.
Just a raw test. SPEED
on a vector of let's say 10,000,000
1. move ctor is 30 times faster than std::copy
2. move (begin, end ,,,) is 7 times faster than std:;copy
3. move_backward (begin, end ,,,) is 13 times faster than std:;copy
What do other people get? (Btw std:list not so impressive).
 
User avatar
Cuchulainn
Posts: 20254
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++ quiz --- generic programming

February 4th, 2017, 7:20 pm

I am trying to define "f > g" for two std::function instances(which means one can define constraints). I tried this, but to no avail:Any ideas?
What about this?
Attachments
TestConstraints.cpp
(1.77 KiB) Downloaded 97 times
 
User avatar
outrun
Posts: 4573
Joined: January 1st, 1970, 12:00 am

Re: C++ quiz --- generic programming

February 4th, 2017, 7:25 pm

Nice. So rolling your own move loop (30x faster) than using the pre-cooked move (7x) or move_backward (13x)? I would expect it to be other way around (slightly) because the functions that get a begin and range can prevent needed reallocation of the vector as it grows.

That's unexpected.

I noticed the backward flips the ordering of the elements, so they are doing different things. Min some cases the ordering doesn't matter, in other cases is does.
 
User avatar
Cuchulainn
Posts: 20254
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++ quiz --- generic programming

February 4th, 2017, 7:40 pm

It's my first serious test. but the move ctor seems to b // Copy vs move an array performance
move ctor is the best. Of course, vector memory is contiguous, so maybe it uses a trick?
//

std::size_t N = 1'000'000;
std::vector<double> v(N);
std::iota(std::begin(v), std::end(v), 1);
std::vector<double> v2;

StopWatch<> sw;
sw.Start();

// Option A (copy elements, linear complexity)
std::copy(std::begin(v), std::end(v), std::back_inserter(v2));

sw.Stop();
std::cout << "Elapsed time copy : " << sw.GetTime() << '\n';
assert(std::equal(std::begin(v), std::end(v), std::begin(v2)));

v2.clear();
sw.Start();

// Option B: Move a range of elements
//std::move(std::begin(v), std::end(v), std::back_inserter(v2));

// Option C: Move constructor
v2 = std::move(v);

sw.Stop();
std::cout << "Elapsed time move: " << sw.GetTime() << '\n';
assert(std::equal(std::begin(v), std::end(v), std::begin(v2)));
std::cout << "Size of input vector: " << v.size() << '\n';

v2.clear();
v = std::vector<double> (N);
std::iota(std::begin(v), std::end(v), 1);
v2.resize(v.size());
sw.Start();

// Option D: move elements to v2, starting at end(v2)
std::move_backward(std::begin(v), std::end(v), std::end(v2));

sw.Stop();
std::cout << "Elapsed time move backward: " << sw.GetTime() << '\n';

assert(std::equal(std::begin(v), std::end(v), std::begin(v2)));
 
User avatar
outrun
Posts: 4573
Joined: January 1st, 1970, 12:00 am

Re: C++ quiz --- generic programming

February 4th, 2017, 8:50 pm

I am trying to define "f > g" for two std::function instances(which means one can define constraints). I tried this, but to no avail:Any ideas?
What about this?
So the goal is to run-time assemble/capture/compose a "callable"

bool h(x)

given a g(x) and f(x) such that

h(x) == [f(x) < g(x)]

?
 
User avatar
Cuchulainn
Posts: 20254
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++ quiz --- generic programming

February 4th, 2017, 10:03 pm

I am trying to define "f > g" for two std::function instances(which means one can define constraints). I tried this, but to no avail:Any ideas?
What about this?
So the goal is to run-time assemble/capture/compose a "callable"

bool h(x)

given a g(x) and f(x) such that

h(x) == [f(x) < g(x)]

?
FunctionType<bool, double> opFun = f > g; // delayed
then opFun(1.0) as in the code sample. run it and see.
 
User avatar
outrun
Posts: 4573
Joined: January 1st, 1970, 12:00 am

Re: C++ quiz --- generic programming

February 4th, 2017, 10:19 pm

v2.clear(); sets the size of the vector to zero, but not the capacity!

So in the first test case you vector does something like this for each push_back:
* the capacity of the vector is C (e.g. 16), the size (number of elements) is N (e.g. 12). If N<C (yes) then we can add one element (there is room for 4 elements), if not then we need to allocate new (larger) memory and move all the elements to that new bigger memory chunk. It typically doubles the capacity after running out of free space so that this costly stuff happens with just logarithmic frequency, so after adding 4 elements it will allocate a new chunk of memory of capacity 32 and copy the 16 elements in there.

In your second text case it will still have a large capacity after clearing (the capacity never shrinks after a clear, a guarantee in the standard) and hence won't waste time doing al this allocating and copying.

one way to fix this is to reverse 1E6 elements in v2 at the top.
 
User avatar
Cuchulainn
Posts: 20254
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++ quiz --- generic programming

February 7th, 2017, 11:44 am

I set up a small experiment to emulate what goes on with one-step finite difference marching schemes for PDE: we have two arrays which hold data at time levels n and n+1, respectively. We has implemented it in several ways using old and new features in C++. The representative results (in seconds) are:
/*
NX: 2000, NT: 10000000
A. standard array way (baseline case) 184.323
B. move of a range 109.535 (C++11)
C. move backwards 62.9273 (C++11)
D. copy v2 = v1 59.0712
E. clear and copy 54.4111
F. define + init arrays in loop 50.975

Cross check (std::accumulate())

aA: 2034.76
aB: 2034.76
aC: 2034.76
aD: 2034.76
aE: 2034.76
aF: 2034.76

*/



I reckon much of code uses approach which for humans is most natural but the compiler has its own ideas on the matter. Some results are counterintuitive at first glance.
 
User avatar
outrun
Posts: 4573
Joined: January 1st, 1970, 12:00 am

Re: C++ quiz --- generic programming

February 7th, 2017, 12:00 pm

and what about swapping the content of the two vectors?

http://www.cplusplus.com/reference/vector/vector/swap/