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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:18:20+00:00 2026-06-04T04:18:20+00:00

I understand that I should not optimize every single point of my program, let’s

  • 0

I understand that I should not optimize every single point of my program, let’s just assume that I have to optimize array initialization.

So I wrote program that compares for loop versus Array.Clear

using System;
using System.Diagnostics;

namespace TestArraysClear
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a = new int[100000];
            Stopwatch sw = Stopwatch.StartNew();
            for (int i = 0; i < 10; i++)
            {
                sw.Reset(); 
                sw.Start();
                for (int j = 0; j < a.Length; j++)
                {
                    a[j] = 0;
                }
                sw.Stop();
                Console.WriteLine("for " + sw.ElapsedTicks);
                sw.Reset();
                sw.Start();
                Array.Clear(a, 0, a.Length);
                sw.Stop();
                Console.WriteLine("Array.Clear " + sw.ElapsedTicks);
            }
        }
    }
}

Output on my machine:

for 1166
Array.Clear 80
for 1136
Array.Clear 91
for 1350
Array.Clear 71
for 1028
Array.Clear 72
for 962
Array.Clear 54
for 1185
Array.Clear 46
for 962
Array.Clear 55
for 1091
Array.Clear 55
for 988
Array.Clear 54
for 1046
Array.Clear 55

So Array.Clear is about 20 times faster than for loop. But Array.Clear initializes to 0. Can I initialize array to -1 with the same perfomance somehow?

upd: I’m not looking for some “extreme unsafe” code. I’m looking for something as easy as Array.Clear. I just wonder that .NET offers fast 0 initialization, but .NET doesn’t offer initialization to other values. So why .NET like “0” much more than “-1”?

upd I want to reset existent array. So i’m looking for analog of Array.Clear which will reset array to -1, not 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-04T04:18:21+00:00Added an answer on June 4, 2026 at 4:18 am

    There’s probably a way to do it via unmanaged arrays (pointers) to set the contiguous block of memory to the same value (e.g. whatever the int ‘-1’ is when mapped into its 4-bytes of memory).

    This article here discusses a faster method than with a managed for loop (example given is a byte[] array):
    http://techmikael.blogspot.com/2009/12/filling-array-with-default-value.html

    Also, here:
    What is the equivalent of memset in C#?

    As other posters have mentioned, it seems a bit extreme to do this though, and normally the speed required to initialise the array using a for loop would not be a problem.

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

Sidebar

Related Questions

I'm a newcomer to Python but I understand that things should not be done
This is a short question. At some point my thread understand that it should
I have been told that scanf should not be used when user inputs a
I understand that Smart::Comments should not be used in production code, since it is
I understand that NSUserDefaults data is unencrypted and should not be used to store
I do understand that functions should not return references to automatic variables. However I
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
I understand that in my unit test I should do BOOST_TEST_MESSAGE(message); instead of std::cout
I'm having trouble with something that I understand should function out of the box
I understand that in the following example a Resume statement should be used instead

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.