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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:20:39+00:00 2026-06-16T06:20:39+00:00

Possible Duplicate: c# Leaner way of initializing int array Basically I would like to

  • 0

Possible Duplicate:
c# Leaner way of initializing int array

Basically I would like to know if there is a more efficent code than the one shown below

    private static int[] GetDefaultSeriesArray(int size, int value)
    {
        int[] result = new int[size];
        for (int i = 0; i < size; i++)
        {
            result[i] = value;
        }
        return result;
    }

where size can vary from 10 to 150000. For small arrays is not an issue, but there should be a better way to do the above.
I am using VS2010(.NET 4.0)

  • 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-16T06:20:40+00:00Added an answer on June 16, 2026 at 6:20 am

    One way that you can improve speed is by utilizing Array.Copy. It’s able to work at a lower level in which it’s bulk assigning larger sections of memory.

    By batching the assignments you can end up copying the array from one section to itself.

    On top of that, the batches themselves can be quite effectively paralleized.

    Here is my initial code up. On my machine (which only has two cores) with a sample array of size 10 million items, I was getting a 15% or so speedup. You’ll need to play around with the batch size (try to stay in multiples of your page size to keep it efficient) to tune it to the size of items that you have. For smaller arrays it’ll end up almost identical to your code as it won’t get past filling up the first batch, but it also won’t be (noticeably) worse in those cases either.

    private const int batchSize = 1048576;
    private static int[] GetDefaultSeriesArray2(int size, int value)
    {
    
        int[] result = new int[size];
    
        //fill the first batch normally
        int end = Math.Min(batchSize, size);
        for (int i = 0; i < end; i++)
        {
            result[i] = value;
        }
    
        int numBatches = size / batchSize;
    
        Parallel.For(1, numBatches, batch =>
        {
            Array.Copy(result, 0, result, batch * batchSize, batchSize);
        });
    
        //handle partial leftover batch
        for (int i = numBatches * batchSize; i < size; i++)
        {
            result[i] = value;
        }
    
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Objective C for Windows iPhone development on Windows Is there any way
Possible Duplicate: Include JavaScript file inside JavaScript file? Is there any way to simply
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: Generating a series of dates What is the best way in mysql
Possible Duplicate: Understanding NSRunLoop Till now I know that every thread has its own
Possible Duplicate: What is the best way to replace or substitute if..else if..else trees
Possible Duplicate: Is there some ninja trick to make a variable constant after its
Possible Duplicate: Can you write object oriented code in C? Hi, can someone point
Possible Duplicate: Reading through file using ifstream I'm trying to find a way to

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.