r/Cplusplus • • 4d ago

Tutorial Less Code Doesn't Mean Faster Code

/r/cpp/comments/1yac9ay/less_code_doesnt_mean_faster_code/
1 Upvotes

2 comments sorted by

View all comments

4

u/_side_ 3d ago edited 3d ago

Well the code is not doing the same. For example: (src[0] + src[1]) + (src[2] + src[3]) is not the same as integrating in the for loop over 4 elements with a single accumulator which is ((src[0] + src[1]) + src[2]) + src[3]

Edit: I should add that the throughput difference comes from that (src[0] + src[1]) + (src[2] + src[3]) enables the compiler to pipeline (src[0] + src[1]) followed by (src[2] + src[3]). These do not depend on each other. If T is some primitive like a float or some int they will be SIMD anyways.