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

  • Home
  • SEARCH
  • 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 6963025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:44:09+00:00 2026-05-27T15:44:09+00:00

I just have tried to compare performance of lambda expressions in C++11, so I

  • 0

I just have tried to compare performance of lambda expressions in C++11, so I did the test — compute sum of elements in a vector of double values. Here is the implementation:

#include <vector>
#include <algorithm>
#include <iostream>
#include <ctime>

#define LOG(x) { std::cout << #x << " = " << (x) << "\n"; }
#define TIME(t) { std::cout << ((double)(clock() - (t)) / CLOCKS_PER_SEC) << " s\n"; }

double sum(const std::vector<double>& v)
{
    double s = 0.0;
    for (auto i = v.cbegin(); i != v.cend(); ++i)
        s += *i;
    return s;
}

int main()
{
    const size_t MAX = 1; // number of tests
    const size_t SIZE = 100000000; // length of the vector

    std::vector<double> v(SIZE, 1.0);
    double out;

    clock_t clk;

    std::cout << "iterator\n";

    clk = clock();
    out = 0.0;
    for (size_t i = 0; i < MAX; ++i)
        out += sum(v);
    TIME(clk)
    LOG(out)

    std::cout << "\nlambda\n";

    clk = clock();
    out = 0.0;
    for (size_t i = 0; i < MAX; ++i)
        std::for_each(v.cbegin(), v.cend(), [&](double d) { out += d; });
    TIME(clk)
    LOG(out)

    return 0;
}

Here is the result of this program (compiled in VS2010 SP1, in Release mode):

iterator
0.32 s
out = 1e+008

lambda
0.326 s
out = 1e+008

As one may see, there is practically no difference in performance. However, if I give 10 as the value of MAX (it means summation will be performed 10 times instead of one), results differ:

iterator
0.287 s
out = 1e+009

lambda
2.84 s
out = 1e+009

Test for lambda expression took approximately 10 times more time. Why? I thought it may be caused by the fact, that on every iteration new lambda is created, but whet I tried this:

out = 0.0;
auto f = [&](double d) { out += d; };
for (size_t i = 0; i < MAX; ++i)
    std::for_each(v.cbegin(), v.cend(), f);

the results hadn’t changed. Could someone explain that behaviour to me?

  • 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-05-27T15:44:09+00:00Added an answer on May 27, 2026 at 3:44 pm

    It turned out that this is not an issue with lambda expressions, just that the compiler optimized-out the outer loop in the first case by caching the result of the sum() function.

    After changing the first case to this form:

    out = 0.0;
    for (size_t i = 0; i < MAX; ++i)
    {
        out += sum(v);
        v[i] = 1.0; // this adds O(1) time and prevents caching
    }
    

    in both cases the timings are approximately equal, with the lambda as a favourite.

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

Sidebar

Related Questions

I have tried just about everything for getting the Kinect for Windows to actually
I have just tried adding a WebService (asmx based) from a WebForms project to
I have just tried using the NSArchiver but for some reason I am getting
I have just tried to uploaded an app through the Application Loader, but it
Update I have just tried Pinning my site to the Taskbar again (after removing
I have tried troubleshooting this problem and just ended up with nothing, I hope
I have tried using a stemmer but the words it produces are just not
I have tried every way I can think of with this, but just not
this is just some pretty standard code I have tried. What I am trying
Iam running WAMP server and just enabled php_memcache extension and have tried the code

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.