Marlschlag falsch&Schlingen

Rayer S’s co-worker exploded into his cube, beaming. “I’ve just optimized our processing loop. I’ve gone from O(n2) to O(n)!”

“That’s amazing !” The processing loop in question was easily the most expensive part of the application, and Rayer had been spending too much time finding ways to squeeze a little performance out of it. “How did you do it?”

The original code looked something like this:

for(int i = 0; i < m; ++i) {
    for(int j = 0; j < n; ++j) {
    	process(target[i][j]); //this is really expensive
    }
}

It was a fairly standard nested loop operation.

Radar pulled the new version from source control to see his co-worker’s genius.

for(int i = 0; i < m * n; ++i) {
    process(*((target*)(&target) + i)); 
}

Radar complimented his co-worker on his understanding of C++ pointers, but had some negative things to say about his understanding of basic arithmetic.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!