By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,515 Members | 1,848 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,515 IT Pros & Developers. It's quick & easy.

Preprocessor to unroll loops

P: n/a
I just found out MSVC++ doesn't unroll loops on any optimization
setting. So I manually unrolled the inner loop of a slow part of my
program... The result was a 2X gain in speed. So I'm wondering about a
way to unroll some other critical loops, other than manually.

Since the number of iterations of the loop can change but is known at
compile time (between 10 and 100), and the work inside the loop is
small, I like to get the preprocessor to unroll the loop all the way if
that's possible. Is it?

Oct 25 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
sp******@yahoo.com wrote:
I just found out MSVC++ doesn't unroll loops on any optimization
setting. So I manually unrolled the inner loop of a slow part of my
program... The result was a 2X gain in speed. So I'm wondering about a
way to unroll some other critical loops, other than manually.

Since the number of iterations of the loop can change but is known at
compile time (between 10 and 100), and the work inside the loop is
small, I like to get the preprocessor to unroll the loop all the way if
that's possible. Is it?


I don't think there is an easy way with the preprocessor. But template
metaprogramming might help here.
Something like:

#include <iostream>

template<int n>
struct loop
{
static void exec()
{
loop<n-1>::exec();
std::cout << "Iteration number " << n << '\n';
}
};

template<>
struct loop<0>
{
static void exec()
{
}
};

int main()
{
loop<10>::exec();
}

Oct 25 '05 #2

P: n/a
REH

sp******@yahoo.com wrote:
I just found out MSVC++ doesn't unroll loops on any optimization
setting. So I manually unrolled the inner loop of a slow part of my
program... The result was a 2X gain in speed. So I'm wondering about a
way to unroll some other critical loops, other than manually.

Since the number of iterations of the loop can change but is known at
compile time (between 10 and 100), and the work inside the loop is
small, I like to get the preprocessor to unroll the loop all the way if
that's possible. Is it?


See the preprocessor library in BOOST. It can do loops and other very
neat stuff.

Oct 25 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.