Skip to main content
GameDev.net gamedev.net
🔒 Locked

New fast way to loop!

Started by martyj2009 Mar 9, 2016 at 11:51 PM 11 replies 6.5k views
Original Post
martyj2009
martyj2009

So there's the for loop. Which works great for most of our programming needs. What if there was a way to speed this up by up to HALF.

Well there is, it's called a five loop.

See below the code example.


// Change this to decide which loop to use!
#define FIVE 0

#if FIVE
#define five for
#endif

#include <time.h>
#include <stdio.h>

int main(int argc, const char *argv[])
{
	int loops = 10000000000;
	int a, b;

	clock_t start, end;
	start = clock();

#if FIVE
	five(int i = 0; i < loops; i++)
#else
	for (int i = 0; i < loops; i++)
#endif
	{
		int a = i;
		b = a * 3;
	}
	
	end = clock();

	double seconds = double(end - start) / CLOCKS_PER_SEC;
	printf("Time Diff: %f", seconds);

	getchar();
}

I know some of you may be doubtful, but the proof is in the pudding.

The for loop results:

QlfMuNz.png

Now compare that to the Five loop

Vw3o2Ix.png

Forget Moore's law. We can't rely on chip manufactures to keep up with our software needs. Five loop is here to save the day!

conq
conq

What if you define five to "ThreadedAsixcronous"?

nfries88
nfries88

This is even faster, guarantee:

#define magic(loopstuff) loopstuff; if(0)

just ignore the compiler warnings about statements with no effect, the compiler just can't handle my magic loops.

It's so fast you'll wonder if your loop is even running at all :)

Mats1
Mats1

Are we really using .h headings?

You can save some time there.

L. Spiro
L. Spiro
I get the same results.
It is confirmed that if you run the for-loop in debug mode and you run the five-loop in release mode you get these results.
Five-loop certainly wins in all cases.


L. Spiro
I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid
ExErvus
ExErvus

Wait, can someone explain what is going on or am I missing a big joke here?

timtom
timtom
Will port all projects to this ASAP!
Dawoodoz
Dawoodoz

If b is written to without being used then a good compiler will cheat and only execute the last iteration of the loop as the first iterations are dead code.

If modified to read the previous result then, the Hexagon compiler for Snapdragon's aDSP would divide the loop into start, multi instruction machine words and termination to run 4 iterations in the same machine instruction with a zero cost hardware loop.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.