SERVING THE QUANTITATIVE FINANCE COMMUNITY

  • 1
  • 6
  • 7
  • 8
  • 9
  • 10
 
User avatar
Cuchulainn
Posts: 59713
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: C++ quiz --- generic programming

February 7th, 2017, 1:11 pm

and what about swapping the content of the two vectors?

http://www.cplusplus.com/reference/vector/vector/swap/
G: std::swap(vold, vnew) .. same performance as F, and in general even better. Couple tests F = 51 seconds, G = 45 seconds.
Last edited by Cuchulainn on February 7th, 2017, 1:26 pm, edited 1 time in total.
 
User avatar
outrun
Posts: 4573
Joined: April 29th, 2016, 1:40 pm

Re: C++ quiz --- generic programming

February 7th, 2017, 1:20 pm

and what about swapping the content of the two vectors?

http://www.cplusplus.com/reference/vector/vector/swap/
G: std::swap(vold, vnew) .. same performance as F, and in general even better. Couple tests F = 51 second., G = 45 seconds.
Thanks for testing!
 
User avatar
outrun
Posts: 4573
Joined: April 29th, 2016, 1:40 pm

Re: C++ quiz --- generic programming

February 7th, 2017, 1:55 pm

I expected it to be fast because it probably swaps the pointers to the memory locations where the elements are stored and not the actual elements.
 
User avatar
Cuchulainn
Posts: 59713
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: C++ quiz --- generic programming

February 7th, 2017, 2:30 pm

You're welcome. Here is another bigger test.

(F is kind of counter-intuitive, but it is very similar to G)

NX: 1000, NT: 100000000
A. standard array way (baseline case) 983.279
B. move of a range 617.602
C. move backwards 382.339
D. copy v2 = v1 361.824
E. clear and copy 360.221
F. define + init arrays in loop 292.115
G. swap option 292.62
aA: 1016.87
aB: 1016.87
aC: 1016.87
aD: 1016.87
aE: 1016.87
aF: 1016.87
aG: 1016.87
 
User avatar
outrun
Posts: 4573
Joined: April 29th, 2016, 1:40 pm

Re: C++ quiz --- generic programming

February 7th, 2017, 2:58 pm

F must be the compiler seeing an optimization opportunity, move it outside the loop and re-use?
 
User avatar
Cuchulainn
Posts: 59713
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: C++ quiz --- generic programming

February 7th, 2017, 3:19 pm

F must be the compiler seeing an optimization opportunity, move it outside the loop and re-use?
I think so. You also it with scalar in a loop.
// V1
double a;
for (i = 1; ...)
{
a = ...
}
// V2
double a;
for (i = 1; ...)
{
double a = ...
}
V2 is faster!
 
User avatar
Cuchulainn
Posts: 59713
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: C++ quiz --- generic programming

February 7th, 2017, 3:20 pm

NX: 5000, NT: 10000000
A. standard array way (baseline case) 487.508
B. move of a range 360.107
C. move backwards 133.317
D. copy v2 = v1 117.96
E. clear and copy 118.093
F. define + init arrays in loop 87.6159
G. swap option 87.1379
aA: 5088.43
aB: 5088.43
aC: 5088.43
aD: 5088.43
aE: 5088.43
aF: 5088.43
aG: 5088.43
 
User avatar
outrun
Posts: 4573
Joined: April 29th, 2016, 1:40 pm

Re: C++ quiz --- generic programming

February 7th, 2017, 3:37 pm

very strange, no?
How do you init inside the loop? With what value, max(S-K,0) ?  That must cost time, no?? What happens if you init the vector with the loop counter value? Does is slow down a lot?
 
User avatar
Cuchulainn
Posts: 59713
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: C++ quiz --- generic programming

February 7th, 2017, 4:42 pm

very strange, no?
How do you init inside the loop? With what value, max(S-K,0) ?  That must cost time, no?? What happens if you init the vector with the loop counter value? Does is slow down a lot?
copy and paste issue.Actually,  I get 
F. define + init arrays in loop 98.3308
G. swap option 127.594
 
User avatar
outrun
Posts: 4573
Joined: April 29th, 2016, 1:40 pm

Re: C++ quiz --- generic programming

February 7th, 2017, 4:52 pm

Thanks for explaining the cause!
 
User avatar
Cuchulainn
Posts: 59713
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: C++ quiz --- generic programming

February 7th, 2017, 5:46 pm

NX: 10'000, NT: 10'000'000
A. standard array way (baseline case) 855.169
B. move of a range 539.237
C. move backwards 273.884
D. copy v2 = v1 313.484
E. clear and copy 260.269
F. define + init arrays in loop 198.506
G. swap option 244.071
aA: 10177.9
aB: 10177.9
aC: 10177.9
aD: 10177.9
aE: 10177.9
aF: 10177.9
aG: 10177.9
 
User avatar
outrun
Posts: 4573
Joined: April 29th, 2016, 1:40 pm

Re: C++ quiz --- generic programming

February 7th, 2017, 6:01 pm

F std::array is probably faster than G std::vector because its size is know compile time and memory is on the stack instead of heap

Can you do the swap on array instead vectors as a new variant :-)

std::swap(array_t,array_t1)

If this is just as fast as G then 1) the speed difference is because of memory and 2) the compiler is smart enough to reuse the array inside the loop. It can do that optimization because it knows the size of that array compile-time and hence can deduce that it can reuse the same array every loop!

If so, then swap is better because that will allow you to declare and access the data *outside* the scope of the loop, which is generally what you want.
 
User avatar
Cuchulainn
Posts: 59713
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: C++ quiz --- generic programming

February 7th, 2017, 10:00 pm

F std::array is probably faster than G std::vector because its size is know compile time and memory is on the stack instead of heap

Can you do the swap on array instead vectors as a new variant :-)

std::swap(array_t,array_t1)

If this is just as fast as G then 1) the speed difference is because of memory and 2) the compiler is smart enough to reuse the array inside the loop. It can do that optimization because it knows the size of that array compile-time and hence can deduce that it can reuse the same array every loop!

If so, then swap is better because that will allow you to declare and access the data *outside* the scope of the loop, which is generally what you want.
option H
NX: 2000, NT: 1000000
A. standard array way (baseline case) 21.8402
B. move of a range 10.266
C. move backwards 5.86059
D. copy v2 = v1 5.55256
E. clear and copy 8.0008
F. define + init arrays in loop 4.40244
G. swap option 5.40454
H. compile-time array 3.69237
aA: 2034.76
aB: 2034.76
aC: 2034.76
aD: 2034.76
aE: 2034.76
aF: 2034.76
aG: 2034.76
aH: 2034.76
 
User avatar
Cuchulainn
Posts: 59713
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: C++ quiz --- generic programming

February 7th, 2017, 10:36 pm

NX: 2000, NT: 10000000
A. standard array way (baseline case) 167.572
B. move of a range 102.667
C. move backwards 63.9424
D. copy v2 = v1 72.1812
E. clear and copy 101.936
F. define + init arrays in loop 94.9325
G. swap option 116.141
H. compile-time array 76.216
aA: 2034.76
aB: 2034.76
aC: 2034.76
aD: 2034.76
aE: 2034.76
aF: 2034.76
aG: 2034.76
aH: 2034.76
 
User avatar
outrun
Posts: 4573
Joined: April 29th, 2016, 1:40 pm

Re: C++ quiz --- generic programming

February 8th, 2017, 6:44 am

This time C and D are best? The ranking looks a bit unpredictable?
ABOUT WILMOTT

PW by JB

Wilmott.com has been "Serving the Quantitative Finance Community" since 2001. Continued...


Twitter LinkedIn Instagram

JOBS BOARD

JOBS BOARD

Looking for a quant job, risk, algo trading,...? Browse jobs here...


GZIP: On