Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7801899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:05:26+00:00 2026-06-02T01:05:26+00:00

I was reading ‘C++ Template complete guide’ book, part about meta programming. There is

  • 0

I was reading ‘C++ Template complete guide’ book, part about meta programming. There is an example of loop unroll (17.7). I’ve implemented the program for dot product calculations:

#include <iostream>
#include <sys/time.h>

using namespace std;

template<int DIM, typename T>
struct Functor
{
    static T dot_product(T *a, T *b)
    {
        return *a * *b + Functor<DIM - 1, T>::dot_product(a + 1, b + 1);
    }
};

template<typename T>
struct Functor<1, T>
{
    static T dot_product(T *a, T *b)
    {
        return *a * *b;
    }
};


template<int DIM, typename T>
T dot_product(T *a, T *b)
{
    return Functor<DIM, T>::dot_product(a, b);
}

double dot_product(int DIM, double *a, double *b)
{
    double res = 0;
    for (int i = 0; i < DIM; ++i)
    {
        res += a[i] * b[i];
    }
    return res;
}


int main(int argc, const char * argv[])
{
    static const int DIM = 100;

    double a[DIM];
    double b[DIM];

    for (int i = 0; i < DIM; ++i)
    {
        a[i] = i;
        b[i] = i;
    }


    {
        timeval startTime;
        gettimeofday(&startTime, 0);

        for (int i = 0; i < 100000; ++i)
        {
            double res = dot_product<DIM>(a, b); 
            //double res = dot_product(DIM, a, b);
        }

        timeval endTime;
        gettimeofday(&endTime, 0);

        double tS = startTime.tv_sec * 1000000 + startTime.tv_usec;
        double tE = endTime.tv_sec   * 1000000 + endTime.tv_usec;

        cout << "template time: " << tE - tS << endl;
    }

    {
        timeval startTime;
        gettimeofday(&startTime, 0);

        for (int i = 0; i < 100000; ++i)
        {
            double res = dot_product(DIM, a, b);
        }

        timeval endTime;
        gettimeofday(&endTime, 0);

        double tS = startTime.tv_sec * 1000000 + startTime.tv_usec;
        double tE = endTime.tv_sec   * 1000000 + endTime.tv_usec;

        cout << "loop time: " << tE - tS << endl;
    }

    return 0;
}

I’m using xcode and I turned all code optimisations off. I expected that template version have to be faster then simple loop according to the book. But the results are (t – Template, l = Loop):

DIM 5: t = ~5000, l = ~3500

DIM 50: t = ~55000, l = 16000

DIM 100: t = 130000, l = 36000

Also i’ve tried to make template functions inline with no performance difference.

Why simple loop is so much faster?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:05:27+00:00Added an answer on June 2, 2026 at 1:05 am

    Depending on the compiler, if you don’t turn on performance optimizations, loop unrolling might not occur.

    It’s pretty easy to understand why: your recursive template instantiations are basically creating a series of functions. The compiler can’t turn all of that into an inlined, unrolled loop and still keep sensible debugging information available. Suppose a segfault happens somewhere inside one of your functions, or an exception is thrown? Wouldn’t you want to be able to get a stack-trace that showed each frame? The compiler thinks you might want that, unless you turn on optimizations, which gives your compiler permission to go to town on your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading C++ Templates: The Complete Guide and it says Note that templates cannot be
Reading MSDN (and other sources) about custom report items (CRI) for reporting services 2005.
Reading through this question on multi-threaded javascript, I was wondering if there would be
Reading Real World Haskell, on page 95 the author provides an example: myFoldl f
Reading over some example Objective C code just now. @property (nonatomic, strong) IBOutlet UILabel
Reading PHP & jQuery Cookbook right now and it gives this example for using
Reading this brief example of using memcached with PHP, I was wondering how memcached
Reading an online resource on PHP about Regexp(TuxRadar). According to the author the following
Reading about the lack of persistence ignorance in Entity Framework I often stumble upon
Reading on SQL Server Book online and my understanding SQL Server Buffer Pool or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.