#include <iostream> using namespace std ; const int BUFFER_SIZE = 50000000 ; double * list = new double [ BUFFER_SIZE ] ; double tightLoop ( ) { // VERSION 1 // double sum = 0.0; // for (int i = 0; i < BUFFER_SIZE; ++i) { // sum += list[i]; // } // return sum; // VERSION 2. 7x faster! double sum = 0.0 ; for ( int i = 0 ; i < BUFFER_SIZE ; i + = 4 ) { sum + = list [ i ] ; sum + = list [ i + 1 ] ; sum + = list [ i + 2 ] ; sum + = list [ i + 3 ] ; } return sum ; // VERSION 3. No more gain than simple loop unrolling // double sum1 = 0.0, sum2 = 0.0, sum3 = 0.0, sum4 = 0.0; // for (int i = 0; i < BUFFER_SIZE; i += 4) { // sum1 += list[i]; // sum2 += list[i + 1]; // sum3 += list[i + 2]; // sum4 += list[i + 3]; // } // return (sum1 + sum2) + (sum3 + sum4); } int main ( ) { double force = 0.0 ; force + = tightLoop ( ) ; force + = tightLoop ( ) ; force + = tightLoop ( ) ; force + = tightLoop ( ) ; force + = tightLoop ( ) ; force + = tightLoop ( ) ; force + = tightLoop ( ) ; cout << force ; delete [ ] list ; return 0 ; }

I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IEJVRkZFUl9TSVpFID0gNTAwMDAwMDA7CmRvdWJsZSogbGlzdCA9IG5ldyBkb3VibGVbQlVGRkVSX1NJWkVdOwoKZG91YmxlIHRpZ2h0TG9vcCgpIHsKCQoJLy8gVkVSU0lPTiAxCgkvLyBkb3VibGUgc3VtID0gMC4wOwoJLy8gZm9yIChpbnQgaSA9IDA7IGkgPCBCVUZGRVJfU0laRTsgKytpKSB7CgkvLyAJc3VtICs9IGxpc3RbaV07CgkvLyB9CgkvLyByZXR1cm4gc3VtOwoKICAgIC8vIFZFUlNJT04gMi4gN3ggZmFzdGVyIQkKCWRvdWJsZSBzdW0gPSAwLjA7Cglmb3IgKGludCBpID0gMDsgaSA8IEJVRkZFUl9TSVpFOyBpKz00KSB7CgkJc3VtICs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