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 8995071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:34:09+00:00 2026-06-15T23:34:09+00:00

I wonder what is the performance difference between fill_n and a for-loop when initializing

  • 0

I wonder what is the performance difference between fill_n and a for-loop when initializing
an array?

Which one is better?

  • 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-15T23:34:09+00:00Added an answer on June 15, 2026 at 11:34 pm

    Just tested these three codes (Compiled by GCC 4.7.2 with -O3 optimization):

    I added these two lines to all the codes, because I think without them the GCC optimizer will deceive me.

    for (int i = 0; i < SIZE; i++)
        arr[i] = arr[i + 1 % SIZE];
    

    1.cpp

    #define SIZE (100*1000*1000)
    char arr[SIZE];
    
    int main()
    {
        for (int i = 0; i < SIZE; i++)
            arr[SIZE] = 0;
    
        for (int i = 0; i < SIZE; i++)
            arr[i] = arr[i + 1 % SIZE];
    }
    

    2.cpp

    ...
        std::fill_n(arr, SIZE, 0);
    ...
    

    3.cpp

    ...
        memset(arr, 0, SIZE);
    ...
    

    Measure times:

    mostafa@debian:~/Codes/tests/x$ time ./1
    real   0m0.073s
    user   0m0.008s
    sys    0m0.064s
    
    mostafa@debian:~/Codes/tests/x$ time ./2
    real   0m0.080s
    user   0m0.020s
    sys    0m0.056s
    
    mostafa@debian:~/Codes/tests/x$ time ./3
    real   0m0.100s
    user   0m0.056s
    sys    0m0.044s
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Every time I create a table I wonder if there is any performance difference
is there any measurably performance difference between ((TypeA) obj).method1(); ((TypeA) obj).method2(); ((TypeA) obj).method3(); and
I wonder if that hurts performance. I have an test app which is pretty
I wonder if there is any difference in performance when accessing a class variable
I wonder about the difference between different data types in SQL Server to store
I wonder if this affects performance or memory consumption a lot. I need an
wonder whether someone can help me with the following one... I have a struct
I'm new to both Web Services and RMI and I wonder which is the
Possible Duplicate: Actual Performance of Fields vs. Properties Is there a known performance difference
Within java I wonder which of these is more efficient: if(something) { if(something) }

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.