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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:59:43+00:00 2026-05-23T16:59:43+00:00

I’ve been working with some programs here at work for about a month now

  • 0

I’ve been working with some programs here at work for about a month now that have a lot of string parsing and such going on. I’ve been advised to use a char array for this stuff as opposed to a string because the char array is faster. I understand why a char array is fast, but what is it about the string type that makes it slower? What data structure is it implementing and is there any way to make it as fast as a char array?

  • 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-23T16:59:45+00:00Added an answer on May 23, 2026 at 4:59 pm

    The most obvious difference is that string is immutable. So you can’t modify parts of it and need to create a completely new copy on each modification.

    String itself has a very special implementation (it’s a variable size class) and is not backed by an array. I see no reason why read-only access to chars in a string should be slow.

    So if you want to change small parts of a string, you need to use either StringBuilder or char[]. Of these two char[] is/was faster since StringBuilder has additional verifications and indirections. But since this is an implementation detail it might have changed since I last tested it.


    Just benchmarked it, and as of .NET 4 setting a member of char[] is about four times as fast compared to a StringBuilder. But both can do more than 200 milion assignments per second, so it rarely matters in practice.

    Reading from a char[] is slightly faster (25% for my test code) that reading from string. Reading from StringBuilder on the other hand is slower (a factor of 3) than reading from char[].

    In all benchmarks I neglected the overhead of my other code. This means that my test underestimates the differences a bit.

    My conclusion is that while char[] is faster than the alternatives it only matters if you’re going over hundreds of megabytes per second.


    //Write StringBuilder
    StringBuilder sb = new StringBuilder();
    sb.Length = 256;
    for(int i=0; i<1000000000; i++)
    {
        int j = i&255;
        sb[j] = 'A';
    }
    
    //Write char[]
    char[] cs = new char[256];
    for(int i=0; i<1000000000; i++)
    {
        int j = i&255;
        cs[j] = 'A';
    }
    
    // Read string
    string s = new String('A',256);
    int sum = 0;
    for(int i=0; i<1000000000; i++)
    {
        int j = i&255;
        sum += s[j];
    }
    
    //Read char[]
    char[] s = new String('A',256).ToCharArray();
    int sum = 0;
    for(int i=0; i<1000000000; i++)
    {
        int j = i&255;
        sum += s[j];
    }
    
    //Read StringBuilder
    StringBuilder s= new StringBuilder(new String('A',256));
    int sum = 0;
    for(int i=0; i<1000000000; i++)
    {
        int j = i&255;
        sum += s[j];
    }
    

    (Yes, I know my benchmark code isn’t very good, but I don’t think it makes a big difference.)

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.